datapath.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. EXPORT_SYMBOL_GPL(ovs_net_id);
  59. static struct genl_family dp_packet_genl_family;
  60. static struct genl_family dp_flow_genl_family;
  61. static struct genl_family dp_datapath_genl_family;
  62. static const struct nla_policy flow_policy[];
  63. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  64. .name = OVS_FLOW_MCGROUP,
  65. };
  66. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  67. .name = OVS_DATAPATH_MCGROUP,
  68. };
  69. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  70. .name = OVS_VPORT_MCGROUP,
  71. };
  72. /* Check if need to build a reply message.
  73. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  74. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  75. unsigned int group)
  76. {
  77. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  78. genl_has_listeners(family, genl_info_net(info), group);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  84. 0, info->nlhdr, GFP_KERNEL);
  85. }
  86. /**
  87. * DOC: Locking:
  88. *
  89. * All writes e.g. Writes to device state (add/remove datapath, port, set
  90. * operations on vports, etc.), Writes to other state (flow table
  91. * modifications, set miscellaneous datapath parameters, etc.) are protected
  92. * by ovs_lock.
  93. *
  94. * Reads are protected by RCU.
  95. *
  96. * There are a few special cases (mostly stats) that have their own
  97. * synchronization but they nest under all of above and don't interact with
  98. * each other.
  99. *
  100. * The RTNL lock nests inside ovs_mutex.
  101. */
  102. static DEFINE_MUTEX(ovs_mutex);
  103. void ovs_lock(void)
  104. {
  105. mutex_lock(&ovs_mutex);
  106. }
  107. void ovs_unlock(void)
  108. {
  109. mutex_unlock(&ovs_mutex);
  110. }
  111. #ifdef CONFIG_LOCKDEP
  112. int lockdep_ovsl_is_held(void)
  113. {
  114. if (debug_locks)
  115. return lockdep_is_held(&ovs_mutex);
  116. else
  117. return 1;
  118. }
  119. EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
  120. #endif
  121. static struct vport *new_vport(const struct vport_parms *);
  122. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  123. const struct sw_flow_key *,
  124. const struct dp_upcall_info *);
  125. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  126. const struct sw_flow_key *,
  127. const struct dp_upcall_info *);
  128. /* Must be called with rcu_read_lock. */
  129. static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  130. {
  131. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  132. if (dev) {
  133. struct vport *vport = ovs_internal_dev_get_vport(dev);
  134. if (vport)
  135. return vport->dp;
  136. }
  137. return NULL;
  138. }
  139. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  140. * returned dp pointer valid.
  141. */
  142. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  143. {
  144. struct datapath *dp;
  145. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  146. rcu_read_lock();
  147. dp = get_dp_rcu(net, dp_ifindex);
  148. rcu_read_unlock();
  149. return dp;
  150. }
  151. /* Must be called with rcu_read_lock or ovs_mutex. */
  152. const char *ovs_dp_name(const struct datapath *dp)
  153. {
  154. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  155. return vport->ops->get_name(vport);
  156. }
  157. static int get_dpifindex(const struct datapath *dp)
  158. {
  159. struct vport *local;
  160. int ifindex;
  161. rcu_read_lock();
  162. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  163. if (local)
  164. ifindex = netdev_vport_priv(local)->dev->ifindex;
  165. else
  166. ifindex = 0;
  167. rcu_read_unlock();
  168. return ifindex;
  169. }
  170. static void destroy_dp_rcu(struct rcu_head *rcu)
  171. {
  172. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  173. ovs_flow_tbl_destroy(&dp->table);
  174. free_percpu(dp->stats_percpu);
  175. kfree(dp->ports);
  176. kfree(dp);
  177. }
  178. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  179. u16 port_no)
  180. {
  181. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  182. }
  183. /* Called with ovs_mutex or RCU read lock. */
  184. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  185. {
  186. struct vport *vport;
  187. struct hlist_head *head;
  188. head = vport_hash_bucket(dp, port_no);
  189. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  190. if (vport->port_no == port_no)
  191. return vport;
  192. }
  193. return NULL;
  194. }
  195. /* Called with ovs_mutex. */
  196. static struct vport *new_vport(const struct vport_parms *parms)
  197. {
  198. struct vport *vport;
  199. vport = ovs_vport_add(parms);
  200. if (!IS_ERR(vport)) {
  201. struct datapath *dp = parms->dp;
  202. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  203. hlist_add_head_rcu(&vport->dp_hash_node, head);
  204. }
  205. return vport;
  206. }
  207. void ovs_dp_detach_port(struct vport *p)
  208. {
  209. ASSERT_OVSL();
  210. /* First drop references to device. */
  211. hlist_del_rcu(&p->dp_hash_node);
  212. /* Then destroy it. */
  213. ovs_vport_del(p);
  214. }
  215. /* Must be called with rcu_read_lock. */
  216. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  217. {
  218. const struct vport *p = OVS_CB(skb)->input_vport;
  219. struct datapath *dp = p->dp;
  220. struct sw_flow *flow;
  221. struct sw_flow_actions *sf_acts;
  222. struct dp_stats_percpu *stats;
  223. u64 *stats_counter;
  224. u32 n_mask_hit;
  225. stats = this_cpu_ptr(dp->stats_percpu);
  226. /* Look up flow. */
  227. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  228. if (unlikely(!flow)) {
  229. struct dp_upcall_info upcall;
  230. int error;
  231. memset(&upcall, 0, sizeof(upcall));
  232. upcall.cmd = OVS_PACKET_CMD_MISS;
  233. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  234. error = ovs_dp_upcall(dp, skb, key, &upcall);
  235. if (unlikely(error))
  236. kfree_skb(skb);
  237. else
  238. consume_skb(skb);
  239. stats_counter = &stats->n_missed;
  240. goto out;
  241. }
  242. ovs_flow_stats_update(flow, key->tp.flags, skb);
  243. sf_acts = rcu_dereference(flow->sf_acts);
  244. ovs_execute_actions(dp, skb, sf_acts, key);
  245. stats_counter = &stats->n_hit;
  246. out:
  247. /* Update datapath statistics. */
  248. u64_stats_update_begin(&stats->syncp);
  249. (*stats_counter)++;
  250. stats->n_mask_hit += n_mask_hit;
  251. u64_stats_update_end(&stats->syncp);
  252. }
  253. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  254. const struct sw_flow_key *key,
  255. const struct dp_upcall_info *upcall_info)
  256. {
  257. struct dp_stats_percpu *stats;
  258. int err;
  259. if (upcall_info->portid == 0) {
  260. err = -ENOTCONN;
  261. goto err;
  262. }
  263. if (!skb_is_gso(skb))
  264. err = queue_userspace_packet(dp, skb, key, upcall_info);
  265. else
  266. err = queue_gso_packets(dp, skb, key, upcall_info);
  267. if (err)
  268. goto err;
  269. return 0;
  270. err:
  271. stats = this_cpu_ptr(dp->stats_percpu);
  272. u64_stats_update_begin(&stats->syncp);
  273. stats->n_lost++;
  274. u64_stats_update_end(&stats->syncp);
  275. return err;
  276. }
  277. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  278. const struct sw_flow_key *key,
  279. const struct dp_upcall_info *upcall_info)
  280. {
  281. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  282. struct sw_flow_key later_key;
  283. struct sk_buff *segs, *nskb;
  284. struct ovs_skb_cb ovs_cb;
  285. int err;
  286. ovs_cb = *OVS_CB(skb);
  287. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  288. *OVS_CB(skb) = ovs_cb;
  289. if (IS_ERR(segs))
  290. return PTR_ERR(segs);
  291. if (segs == NULL)
  292. return -EINVAL;
  293. if (gso_type & SKB_GSO_UDP) {
  294. /* The initial flow key extracted by ovs_flow_key_extract()
  295. * in this case is for a first fragment, so we need to
  296. * properly mark later fragments.
  297. */
  298. later_key = *key;
  299. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  300. }
  301. /* Queue all of the segments. */
  302. skb = segs;
  303. do {
  304. *OVS_CB(skb) = ovs_cb;
  305. if (gso_type & SKB_GSO_UDP && skb != segs)
  306. key = &later_key;
  307. err = queue_userspace_packet(dp, skb, key, upcall_info);
  308. if (err)
  309. break;
  310. } while ((skb = skb->next));
  311. /* Free all of the segments. */
  312. skb = segs;
  313. do {
  314. nskb = skb->next;
  315. if (err)
  316. kfree_skb(skb);
  317. else
  318. consume_skb(skb);
  319. } while ((skb = nskb));
  320. return err;
  321. }
  322. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  323. unsigned int hdrlen)
  324. {
  325. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  326. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  327. + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  328. /* OVS_PACKET_ATTR_USERDATA */
  329. if (upcall_info->userdata)
  330. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  331. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  332. if (upcall_info->egress_tun_info)
  333. size += nla_total_size(ovs_tun_key_attr_size());
  334. /* OVS_PACKET_ATTR_ACTIONS */
  335. if (upcall_info->actions_len)
  336. size += nla_total_size(upcall_info->actions_len);
  337. return size;
  338. }
  339. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  340. const struct sw_flow_key *key,
  341. const struct dp_upcall_info *upcall_info)
  342. {
  343. struct ovs_header *upcall;
  344. struct sk_buff *nskb = NULL;
  345. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  346. struct nlattr *nla;
  347. struct genl_info info = {
  348. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  349. .snd_portid = upcall_info->portid,
  350. };
  351. size_t len;
  352. unsigned int hlen;
  353. int err, dp_ifindex;
  354. dp_ifindex = get_dpifindex(dp);
  355. if (!dp_ifindex)
  356. return -ENODEV;
  357. if (skb_vlan_tag_present(skb)) {
  358. nskb = skb_clone(skb, GFP_ATOMIC);
  359. if (!nskb)
  360. return -ENOMEM;
  361. nskb = __vlan_hwaccel_push_inside(nskb);
  362. if (!nskb)
  363. return -ENOMEM;
  364. skb = nskb;
  365. }
  366. if (nla_attr_size(skb->len) > USHRT_MAX) {
  367. err = -EFBIG;
  368. goto out;
  369. }
  370. /* Complete checksum if needed */
  371. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  372. (err = skb_checksum_help(skb)))
  373. goto out;
  374. /* Older versions of OVS user space enforce alignment of the last
  375. * Netlink attribute to NLA_ALIGNTO which would require extensive
  376. * padding logic. Only perform zerocopy if padding is not required.
  377. */
  378. if (dp->user_features & OVS_DP_F_UNALIGNED)
  379. hlen = skb_zerocopy_headlen(skb);
  380. else
  381. hlen = skb->len;
  382. len = upcall_msg_size(upcall_info, hlen);
  383. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  384. if (!user_skb) {
  385. err = -ENOMEM;
  386. goto out;
  387. }
  388. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  389. 0, upcall_info->cmd);
  390. upcall->dp_ifindex = dp_ifindex;
  391. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  392. BUG_ON(err);
  393. if (upcall_info->userdata)
  394. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  395. nla_len(upcall_info->userdata),
  396. nla_data(upcall_info->userdata));
  397. if (upcall_info->egress_tun_info) {
  398. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  399. err = ovs_nla_put_egress_tunnel_key(user_skb,
  400. upcall_info->egress_tun_info);
  401. BUG_ON(err);
  402. nla_nest_end(user_skb, nla);
  403. }
  404. if (upcall_info->actions_len) {
  405. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
  406. err = ovs_nla_put_actions(upcall_info->actions,
  407. upcall_info->actions_len,
  408. user_skb);
  409. if (!err)
  410. nla_nest_end(user_skb, nla);
  411. else
  412. nla_nest_cancel(user_skb, nla);
  413. }
  414. /* Only reserve room for attribute header, packet data is added
  415. * in skb_zerocopy() */
  416. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  417. err = -ENOBUFS;
  418. goto out;
  419. }
  420. nla->nla_len = nla_attr_size(skb->len);
  421. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  422. if (err)
  423. goto out;
  424. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  425. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  426. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  427. if (plen > 0)
  428. memset(skb_put(user_skb, plen), 0, plen);
  429. }
  430. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  431. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  432. user_skb = NULL;
  433. out:
  434. if (err)
  435. skb_tx_error(skb);
  436. kfree_skb(user_skb);
  437. kfree_skb(nskb);
  438. return err;
  439. }
  440. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  441. {
  442. struct ovs_header *ovs_header = info->userhdr;
  443. struct nlattr **a = info->attrs;
  444. struct sw_flow_actions *acts;
  445. struct sk_buff *packet;
  446. struct sw_flow *flow;
  447. struct sw_flow_actions *sf_acts;
  448. struct datapath *dp;
  449. struct ethhdr *eth;
  450. struct vport *input_vport;
  451. int len;
  452. int err;
  453. bool log = !a[OVS_PACKET_ATTR_PROBE];
  454. err = -EINVAL;
  455. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  456. !a[OVS_PACKET_ATTR_ACTIONS])
  457. goto err;
  458. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  459. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  460. err = -ENOMEM;
  461. if (!packet)
  462. goto err;
  463. skb_reserve(packet, NET_IP_ALIGN);
  464. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  465. skb_reset_mac_header(packet);
  466. eth = eth_hdr(packet);
  467. /* Normally, setting the skb 'protocol' field would be handled by a
  468. * call to eth_type_trans(), but it assumes there's a sending
  469. * device, which we may not have. */
  470. if (eth_proto_is_802_3(eth->h_proto))
  471. packet->protocol = eth->h_proto;
  472. else
  473. packet->protocol = htons(ETH_P_802_2);
  474. /* Build an sw_flow for sending this packet. */
  475. flow = ovs_flow_alloc();
  476. err = PTR_ERR(flow);
  477. if (IS_ERR(flow))
  478. goto err_kfree_skb;
  479. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  480. &flow->key, log);
  481. if (err)
  482. goto err_flow_free;
  483. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  484. &flow->key, &acts, log);
  485. if (err)
  486. goto err_flow_free;
  487. rcu_assign_pointer(flow->sf_acts, acts);
  488. OVS_CB(packet)->egress_tun_info = NULL;
  489. packet->priority = flow->key.phy.priority;
  490. packet->mark = flow->key.phy.skb_mark;
  491. rcu_read_lock();
  492. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  493. err = -ENODEV;
  494. if (!dp)
  495. goto err_unlock;
  496. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  497. if (!input_vport)
  498. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  499. if (!input_vport)
  500. goto err_unlock;
  501. OVS_CB(packet)->input_vport = input_vport;
  502. sf_acts = rcu_dereference(flow->sf_acts);
  503. local_bh_disable();
  504. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  505. local_bh_enable();
  506. rcu_read_unlock();
  507. ovs_flow_free(flow, false);
  508. return err;
  509. err_unlock:
  510. rcu_read_unlock();
  511. err_flow_free:
  512. ovs_flow_free(flow, false);
  513. err_kfree_skb:
  514. kfree_skb(packet);
  515. err:
  516. return err;
  517. }
  518. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  519. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  520. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  521. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  522. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  523. };
  524. static const struct genl_ops dp_packet_genl_ops[] = {
  525. { .cmd = OVS_PACKET_CMD_EXECUTE,
  526. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  527. .policy = packet_policy,
  528. .doit = ovs_packet_cmd_execute
  529. }
  530. };
  531. static struct genl_family dp_packet_genl_family = {
  532. .id = GENL_ID_GENERATE,
  533. .hdrsize = sizeof(struct ovs_header),
  534. .name = OVS_PACKET_FAMILY,
  535. .version = OVS_PACKET_VERSION,
  536. .maxattr = OVS_PACKET_ATTR_MAX,
  537. .netnsok = true,
  538. .parallel_ops = true,
  539. .ops = dp_packet_genl_ops,
  540. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  541. };
  542. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  543. struct ovs_dp_megaflow_stats *mega_stats)
  544. {
  545. int i;
  546. memset(mega_stats, 0, sizeof(*mega_stats));
  547. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  548. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  549. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  550. for_each_possible_cpu(i) {
  551. const struct dp_stats_percpu *percpu_stats;
  552. struct dp_stats_percpu local_stats;
  553. unsigned int start;
  554. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  555. do {
  556. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  557. local_stats = *percpu_stats;
  558. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  559. stats->n_hit += local_stats.n_hit;
  560. stats->n_missed += local_stats.n_missed;
  561. stats->n_lost += local_stats.n_lost;
  562. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  563. }
  564. }
  565. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  566. {
  567. return ovs_identifier_is_ufid(sfid) &&
  568. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  569. }
  570. static bool should_fill_mask(uint32_t ufid_flags)
  571. {
  572. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  573. }
  574. static bool should_fill_actions(uint32_t ufid_flags)
  575. {
  576. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  577. }
  578. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  579. const struct sw_flow_id *sfid,
  580. uint32_t ufid_flags)
  581. {
  582. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  583. /* OVS_FLOW_ATTR_UFID */
  584. if (sfid && ovs_identifier_is_ufid(sfid))
  585. len += nla_total_size(sfid->ufid_len);
  586. /* OVS_FLOW_ATTR_KEY */
  587. if (!sfid || should_fill_key(sfid, ufid_flags))
  588. len += nla_total_size(ovs_key_attr_size());
  589. /* OVS_FLOW_ATTR_MASK */
  590. if (should_fill_mask(ufid_flags))
  591. len += nla_total_size(ovs_key_attr_size());
  592. /* OVS_FLOW_ATTR_ACTIONS */
  593. if (should_fill_actions(ufid_flags))
  594. len += nla_total_size(acts->actions_len);
  595. return len
  596. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  597. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  598. + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
  599. }
  600. /* Called with ovs_mutex or RCU read lock. */
  601. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  602. struct sk_buff *skb)
  603. {
  604. struct ovs_flow_stats stats;
  605. __be16 tcp_flags;
  606. unsigned long used;
  607. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  608. if (used &&
  609. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  610. return -EMSGSIZE;
  611. if (stats.n_packets &&
  612. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  613. return -EMSGSIZE;
  614. if ((u8)ntohs(tcp_flags) &&
  615. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  616. return -EMSGSIZE;
  617. return 0;
  618. }
  619. /* Called with ovs_mutex or RCU read lock. */
  620. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  621. struct sk_buff *skb, int skb_orig_len)
  622. {
  623. struct nlattr *start;
  624. int err;
  625. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  626. * this is the first flow to be dumped into 'skb'. This is unusual for
  627. * Netlink but individual action lists can be longer than
  628. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  629. * The userspace caller can always fetch the actions separately if it
  630. * really wants them. (Most userspace callers in fact don't care.)
  631. *
  632. * This can only fail for dump operations because the skb is always
  633. * properly sized for single flows.
  634. */
  635. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  636. if (start) {
  637. const struct sw_flow_actions *sf_acts;
  638. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  639. err = ovs_nla_put_actions(sf_acts->actions,
  640. sf_acts->actions_len, skb);
  641. if (!err)
  642. nla_nest_end(skb, start);
  643. else {
  644. if (skb_orig_len)
  645. return err;
  646. nla_nest_cancel(skb, start);
  647. }
  648. } else if (skb_orig_len) {
  649. return -EMSGSIZE;
  650. }
  651. return 0;
  652. }
  653. /* Called with ovs_mutex or RCU read lock. */
  654. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  655. struct sk_buff *skb, u32 portid,
  656. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  657. {
  658. const int skb_orig_len = skb->len;
  659. struct ovs_header *ovs_header;
  660. int err;
  661. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  662. flags, cmd);
  663. if (!ovs_header)
  664. return -EMSGSIZE;
  665. ovs_header->dp_ifindex = dp_ifindex;
  666. err = ovs_nla_put_identifier(flow, skb);
  667. if (err)
  668. goto error;
  669. if (should_fill_key(&flow->id, ufid_flags)) {
  670. err = ovs_nla_put_masked_key(flow, skb);
  671. if (err)
  672. goto error;
  673. }
  674. if (should_fill_mask(ufid_flags)) {
  675. err = ovs_nla_put_mask(flow, skb);
  676. if (err)
  677. goto error;
  678. }
  679. err = ovs_flow_cmd_fill_stats(flow, skb);
  680. if (err)
  681. goto error;
  682. if (should_fill_actions(ufid_flags)) {
  683. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  684. if (err)
  685. goto error;
  686. }
  687. genlmsg_end(skb, ovs_header);
  688. return 0;
  689. error:
  690. genlmsg_cancel(skb, ovs_header);
  691. return err;
  692. }
  693. /* May not be called with RCU read lock. */
  694. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  695. const struct sw_flow_id *sfid,
  696. struct genl_info *info,
  697. bool always,
  698. uint32_t ufid_flags)
  699. {
  700. struct sk_buff *skb;
  701. size_t len;
  702. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  703. return NULL;
  704. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  705. skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
  706. if (!skb)
  707. return ERR_PTR(-ENOMEM);
  708. return skb;
  709. }
  710. /* Called with ovs_mutex. */
  711. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  712. int dp_ifindex,
  713. struct genl_info *info, u8 cmd,
  714. bool always, u32 ufid_flags)
  715. {
  716. struct sk_buff *skb;
  717. int retval;
  718. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  719. &flow->id, info, always, ufid_flags);
  720. if (IS_ERR_OR_NULL(skb))
  721. return skb;
  722. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  723. info->snd_portid, info->snd_seq, 0,
  724. cmd, ufid_flags);
  725. BUG_ON(retval < 0);
  726. return skb;
  727. }
  728. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  729. {
  730. struct nlattr **a = info->attrs;
  731. struct ovs_header *ovs_header = info->userhdr;
  732. struct sw_flow *flow = NULL, *new_flow;
  733. struct sw_flow_mask mask;
  734. struct sk_buff *reply;
  735. struct datapath *dp;
  736. struct sw_flow_key key;
  737. struct sw_flow_actions *acts;
  738. struct sw_flow_match match;
  739. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  740. int error;
  741. bool log = !a[OVS_FLOW_ATTR_PROBE];
  742. /* Must have key and actions. */
  743. error = -EINVAL;
  744. if (!a[OVS_FLOW_ATTR_KEY]) {
  745. OVS_NLERR(log, "Flow key attr not present in new flow.");
  746. goto error;
  747. }
  748. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  749. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  750. goto error;
  751. }
  752. /* Most of the time we need to allocate a new flow, do it before
  753. * locking.
  754. */
  755. new_flow = ovs_flow_alloc();
  756. if (IS_ERR(new_flow)) {
  757. error = PTR_ERR(new_flow);
  758. goto error;
  759. }
  760. /* Extract key. */
  761. ovs_match_init(&match, &key, &mask);
  762. error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
  763. a[OVS_FLOW_ATTR_MASK], log);
  764. if (error)
  765. goto err_kfree_flow;
  766. ovs_flow_mask_key(&new_flow->key, &key, &mask);
  767. /* Extract flow identifier. */
  768. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  769. &key, log);
  770. if (error)
  771. goto err_kfree_flow;
  772. /* Validate actions. */
  773. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  774. &acts, log);
  775. if (error) {
  776. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  777. goto err_kfree_flow;
  778. }
  779. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  780. ufid_flags);
  781. if (IS_ERR(reply)) {
  782. error = PTR_ERR(reply);
  783. goto err_kfree_acts;
  784. }
  785. ovs_lock();
  786. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  787. if (unlikely(!dp)) {
  788. error = -ENODEV;
  789. goto err_unlock_ovs;
  790. }
  791. /* Check if this is a duplicate flow */
  792. if (ovs_identifier_is_ufid(&new_flow->id))
  793. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  794. if (!flow)
  795. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  796. if (likely(!flow)) {
  797. rcu_assign_pointer(new_flow->sf_acts, acts);
  798. /* Put flow in bucket. */
  799. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  800. if (unlikely(error)) {
  801. acts = NULL;
  802. goto err_unlock_ovs;
  803. }
  804. if (unlikely(reply)) {
  805. error = ovs_flow_cmd_fill_info(new_flow,
  806. ovs_header->dp_ifindex,
  807. reply, info->snd_portid,
  808. info->snd_seq, 0,
  809. OVS_FLOW_CMD_NEW,
  810. ufid_flags);
  811. BUG_ON(error < 0);
  812. }
  813. ovs_unlock();
  814. } else {
  815. struct sw_flow_actions *old_acts;
  816. /* Bail out if we're not allowed to modify an existing flow.
  817. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  818. * because Generic Netlink treats the latter as a dump
  819. * request. We also accept NLM_F_EXCL in case that bug ever
  820. * gets fixed.
  821. */
  822. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  823. | NLM_F_EXCL))) {
  824. error = -EEXIST;
  825. goto err_unlock_ovs;
  826. }
  827. /* The flow identifier has to be the same for flow updates.
  828. * Look for any overlapping flow.
  829. */
  830. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  831. if (ovs_identifier_is_key(&flow->id))
  832. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  833. &match);
  834. else /* UFID matches but key is different */
  835. flow = NULL;
  836. if (!flow) {
  837. error = -ENOENT;
  838. goto err_unlock_ovs;
  839. }
  840. }
  841. /* Update actions. */
  842. old_acts = ovsl_dereference(flow->sf_acts);
  843. rcu_assign_pointer(flow->sf_acts, acts);
  844. if (unlikely(reply)) {
  845. error = ovs_flow_cmd_fill_info(flow,
  846. ovs_header->dp_ifindex,
  847. reply, info->snd_portid,
  848. info->snd_seq, 0,
  849. OVS_FLOW_CMD_NEW,
  850. ufid_flags);
  851. BUG_ON(error < 0);
  852. }
  853. ovs_unlock();
  854. ovs_nla_free_flow_actions(old_acts);
  855. ovs_flow_free(new_flow, false);
  856. }
  857. if (reply)
  858. ovs_notify(&dp_flow_genl_family, reply, info);
  859. return 0;
  860. err_unlock_ovs:
  861. ovs_unlock();
  862. kfree_skb(reply);
  863. err_kfree_acts:
  864. kfree(acts);
  865. err_kfree_flow:
  866. ovs_flow_free(new_flow, false);
  867. error:
  868. return error;
  869. }
  870. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  871. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  872. const struct sw_flow_key *key,
  873. const struct sw_flow_mask *mask,
  874. bool log)
  875. {
  876. struct sw_flow_actions *acts;
  877. struct sw_flow_key masked_key;
  878. int error;
  879. ovs_flow_mask_key(&masked_key, key, mask);
  880. error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
  881. if (error) {
  882. OVS_NLERR(log,
  883. "Actions may not be safe on all matching packets");
  884. return ERR_PTR(error);
  885. }
  886. return acts;
  887. }
  888. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  889. {
  890. struct nlattr **a = info->attrs;
  891. struct ovs_header *ovs_header = info->userhdr;
  892. struct sw_flow_key key;
  893. struct sw_flow *flow;
  894. struct sw_flow_mask mask;
  895. struct sk_buff *reply = NULL;
  896. struct datapath *dp;
  897. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  898. struct sw_flow_match match;
  899. struct sw_flow_id sfid;
  900. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  901. int error;
  902. bool log = !a[OVS_FLOW_ATTR_PROBE];
  903. bool ufid_present;
  904. /* Extract key. */
  905. error = -EINVAL;
  906. if (!a[OVS_FLOW_ATTR_KEY]) {
  907. OVS_NLERR(log, "Flow key attribute not present in set flow.");
  908. goto error;
  909. }
  910. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  911. ovs_match_init(&match, &key, &mask);
  912. error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
  913. a[OVS_FLOW_ATTR_MASK], log);
  914. if (error)
  915. goto error;
  916. /* Validate actions. */
  917. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  918. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
  919. log);
  920. if (IS_ERR(acts)) {
  921. error = PTR_ERR(acts);
  922. goto error;
  923. }
  924. /* Can allocate before locking if have acts. */
  925. reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
  926. ufid_flags);
  927. if (IS_ERR(reply)) {
  928. error = PTR_ERR(reply);
  929. goto err_kfree_acts;
  930. }
  931. }
  932. ovs_lock();
  933. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  934. if (unlikely(!dp)) {
  935. error = -ENODEV;
  936. goto err_unlock_ovs;
  937. }
  938. /* Check that the flow exists. */
  939. if (ufid_present)
  940. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
  941. else
  942. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  943. if (unlikely(!flow)) {
  944. error = -ENOENT;
  945. goto err_unlock_ovs;
  946. }
  947. /* Update actions, if present. */
  948. if (likely(acts)) {
  949. old_acts = ovsl_dereference(flow->sf_acts);
  950. rcu_assign_pointer(flow->sf_acts, acts);
  951. if (unlikely(reply)) {
  952. error = ovs_flow_cmd_fill_info(flow,
  953. ovs_header->dp_ifindex,
  954. reply, info->snd_portid,
  955. info->snd_seq, 0,
  956. OVS_FLOW_CMD_NEW,
  957. ufid_flags);
  958. BUG_ON(error < 0);
  959. }
  960. } else {
  961. /* Could not alloc without acts before locking. */
  962. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  963. info, OVS_FLOW_CMD_NEW, false,
  964. ufid_flags);
  965. if (unlikely(IS_ERR(reply))) {
  966. error = PTR_ERR(reply);
  967. goto err_unlock_ovs;
  968. }
  969. }
  970. /* Clear stats. */
  971. if (a[OVS_FLOW_ATTR_CLEAR])
  972. ovs_flow_stats_clear(flow);
  973. ovs_unlock();
  974. if (reply)
  975. ovs_notify(&dp_flow_genl_family, reply, info);
  976. if (old_acts)
  977. ovs_nla_free_flow_actions(old_acts);
  978. return 0;
  979. err_unlock_ovs:
  980. ovs_unlock();
  981. kfree_skb(reply);
  982. err_kfree_acts:
  983. kfree(acts);
  984. error:
  985. return error;
  986. }
  987. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  988. {
  989. struct nlattr **a = info->attrs;
  990. struct ovs_header *ovs_header = info->userhdr;
  991. struct sw_flow_key key;
  992. struct sk_buff *reply;
  993. struct sw_flow *flow;
  994. struct datapath *dp;
  995. struct sw_flow_match match;
  996. struct sw_flow_id ufid;
  997. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  998. int err = 0;
  999. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1000. bool ufid_present;
  1001. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1002. if (a[OVS_FLOW_ATTR_KEY]) {
  1003. ovs_match_init(&match, &key, NULL);
  1004. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
  1005. log);
  1006. } else if (!ufid_present) {
  1007. OVS_NLERR(log,
  1008. "Flow get message rejected, Key attribute missing.");
  1009. err = -EINVAL;
  1010. }
  1011. if (err)
  1012. return err;
  1013. ovs_lock();
  1014. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1015. if (!dp) {
  1016. err = -ENODEV;
  1017. goto unlock;
  1018. }
  1019. if (ufid_present)
  1020. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1021. else
  1022. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1023. if (!flow) {
  1024. err = -ENOENT;
  1025. goto unlock;
  1026. }
  1027. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  1028. OVS_FLOW_CMD_NEW, true, ufid_flags);
  1029. if (IS_ERR(reply)) {
  1030. err = PTR_ERR(reply);
  1031. goto unlock;
  1032. }
  1033. ovs_unlock();
  1034. return genlmsg_reply(reply, info);
  1035. unlock:
  1036. ovs_unlock();
  1037. return err;
  1038. }
  1039. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1040. {
  1041. struct nlattr **a = info->attrs;
  1042. struct ovs_header *ovs_header = info->userhdr;
  1043. struct sw_flow_key key;
  1044. struct sk_buff *reply;
  1045. struct sw_flow *flow = NULL;
  1046. struct datapath *dp;
  1047. struct sw_flow_match match;
  1048. struct sw_flow_id ufid;
  1049. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1050. int err;
  1051. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1052. bool ufid_present;
  1053. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1054. if (a[OVS_FLOW_ATTR_KEY]) {
  1055. ovs_match_init(&match, &key, NULL);
  1056. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
  1057. log);
  1058. if (unlikely(err))
  1059. return err;
  1060. }
  1061. ovs_lock();
  1062. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1063. if (unlikely(!dp)) {
  1064. err = -ENODEV;
  1065. goto unlock;
  1066. }
  1067. if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
  1068. err = ovs_flow_tbl_flush(&dp->table);
  1069. goto unlock;
  1070. }
  1071. if (ufid_present)
  1072. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1073. else
  1074. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1075. if (unlikely(!flow)) {
  1076. err = -ENOENT;
  1077. goto unlock;
  1078. }
  1079. ovs_flow_tbl_remove(&dp->table, flow);
  1080. ovs_unlock();
  1081. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1082. &flow->id, info, false, ufid_flags);
  1083. if (likely(reply)) {
  1084. if (likely(!IS_ERR(reply))) {
  1085. rcu_read_lock(); /*To keep RCU checker happy. */
  1086. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1087. reply, info->snd_portid,
  1088. info->snd_seq, 0,
  1089. OVS_FLOW_CMD_DEL,
  1090. ufid_flags);
  1091. rcu_read_unlock();
  1092. BUG_ON(err < 0);
  1093. ovs_notify(&dp_flow_genl_family, reply, info);
  1094. } else {
  1095. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1096. }
  1097. }
  1098. ovs_flow_free(flow, true);
  1099. return 0;
  1100. unlock:
  1101. ovs_unlock();
  1102. return err;
  1103. }
  1104. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1105. {
  1106. struct nlattr *a[__OVS_FLOW_ATTR_MAX];
  1107. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1108. struct table_instance *ti;
  1109. struct datapath *dp;
  1110. u32 ufid_flags;
  1111. int err;
  1112. err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
  1113. OVS_FLOW_ATTR_MAX, flow_policy);
  1114. if (err)
  1115. return err;
  1116. ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1117. rcu_read_lock();
  1118. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1119. if (!dp) {
  1120. rcu_read_unlock();
  1121. return -ENODEV;
  1122. }
  1123. ti = rcu_dereference(dp->table.ti);
  1124. for (;;) {
  1125. struct sw_flow *flow;
  1126. u32 bucket, obj;
  1127. bucket = cb->args[0];
  1128. obj = cb->args[1];
  1129. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1130. if (!flow)
  1131. break;
  1132. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1133. NETLINK_CB(cb->skb).portid,
  1134. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1135. OVS_FLOW_CMD_NEW, ufid_flags) < 0)
  1136. break;
  1137. cb->args[0] = bucket;
  1138. cb->args[1] = obj;
  1139. }
  1140. rcu_read_unlock();
  1141. return skb->len;
  1142. }
  1143. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1144. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1145. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1146. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1147. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1148. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1149. [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
  1150. [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
  1151. };
  1152. static const struct genl_ops dp_flow_genl_ops[] = {
  1153. { .cmd = OVS_FLOW_CMD_NEW,
  1154. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1155. .policy = flow_policy,
  1156. .doit = ovs_flow_cmd_new
  1157. },
  1158. { .cmd = OVS_FLOW_CMD_DEL,
  1159. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1160. .policy = flow_policy,
  1161. .doit = ovs_flow_cmd_del
  1162. },
  1163. { .cmd = OVS_FLOW_CMD_GET,
  1164. .flags = 0, /* OK for unprivileged users. */
  1165. .policy = flow_policy,
  1166. .doit = ovs_flow_cmd_get,
  1167. .dumpit = ovs_flow_cmd_dump
  1168. },
  1169. { .cmd = OVS_FLOW_CMD_SET,
  1170. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1171. .policy = flow_policy,
  1172. .doit = ovs_flow_cmd_set,
  1173. },
  1174. };
  1175. static struct genl_family dp_flow_genl_family = {
  1176. .id = GENL_ID_GENERATE,
  1177. .hdrsize = sizeof(struct ovs_header),
  1178. .name = OVS_FLOW_FAMILY,
  1179. .version = OVS_FLOW_VERSION,
  1180. .maxattr = OVS_FLOW_ATTR_MAX,
  1181. .netnsok = true,
  1182. .parallel_ops = true,
  1183. .ops = dp_flow_genl_ops,
  1184. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1185. .mcgrps = &ovs_dp_flow_multicast_group,
  1186. .n_mcgrps = 1,
  1187. };
  1188. static size_t ovs_dp_cmd_msg_size(void)
  1189. {
  1190. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1191. msgsize += nla_total_size(IFNAMSIZ);
  1192. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1193. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1194. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1195. return msgsize;
  1196. }
  1197. /* Called with ovs_mutex. */
  1198. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1199. u32 portid, u32 seq, u32 flags, u8 cmd)
  1200. {
  1201. struct ovs_header *ovs_header;
  1202. struct ovs_dp_stats dp_stats;
  1203. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1204. int err;
  1205. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1206. flags, cmd);
  1207. if (!ovs_header)
  1208. goto error;
  1209. ovs_header->dp_ifindex = get_dpifindex(dp);
  1210. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1211. if (err)
  1212. goto nla_put_failure;
  1213. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1214. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1215. &dp_stats))
  1216. goto nla_put_failure;
  1217. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1218. sizeof(struct ovs_dp_megaflow_stats),
  1219. &dp_megaflow_stats))
  1220. goto nla_put_failure;
  1221. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1222. goto nla_put_failure;
  1223. genlmsg_end(skb, ovs_header);
  1224. return 0;
  1225. nla_put_failure:
  1226. genlmsg_cancel(skb, ovs_header);
  1227. error:
  1228. return -EMSGSIZE;
  1229. }
  1230. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1231. {
  1232. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1233. }
  1234. /* Called with rcu_read_lock or ovs_mutex. */
  1235. static struct datapath *lookup_datapath(struct net *net,
  1236. const struct ovs_header *ovs_header,
  1237. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1238. {
  1239. struct datapath *dp;
  1240. if (!a[OVS_DP_ATTR_NAME])
  1241. dp = get_dp(net, ovs_header->dp_ifindex);
  1242. else {
  1243. struct vport *vport;
  1244. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1245. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1246. }
  1247. return dp ? dp : ERR_PTR(-ENODEV);
  1248. }
  1249. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1250. {
  1251. struct datapath *dp;
  1252. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1253. if (IS_ERR(dp))
  1254. return;
  1255. WARN(dp->user_features, "Dropping previously announced user features\n");
  1256. dp->user_features = 0;
  1257. }
  1258. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1259. {
  1260. if (a[OVS_DP_ATTR_USER_FEATURES])
  1261. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1262. }
  1263. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1264. {
  1265. struct nlattr **a = info->attrs;
  1266. struct vport_parms parms;
  1267. struct sk_buff *reply;
  1268. struct datapath *dp;
  1269. struct vport *vport;
  1270. struct ovs_net *ovs_net;
  1271. int err, i;
  1272. err = -EINVAL;
  1273. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1274. goto err;
  1275. reply = ovs_dp_cmd_alloc_info(info);
  1276. if (!reply)
  1277. return -ENOMEM;
  1278. err = -ENOMEM;
  1279. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1280. if (dp == NULL)
  1281. goto err_free_reply;
  1282. ovs_dp_set_net(dp, sock_net(skb->sk));
  1283. /* Allocate table. */
  1284. err = ovs_flow_tbl_init(&dp->table);
  1285. if (err)
  1286. goto err_free_dp;
  1287. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1288. if (!dp->stats_percpu) {
  1289. err = -ENOMEM;
  1290. goto err_destroy_table;
  1291. }
  1292. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1293. GFP_KERNEL);
  1294. if (!dp->ports) {
  1295. err = -ENOMEM;
  1296. goto err_destroy_percpu;
  1297. }
  1298. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1299. INIT_HLIST_HEAD(&dp->ports[i]);
  1300. /* Set up our datapath device. */
  1301. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1302. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1303. parms.options = NULL;
  1304. parms.dp = dp;
  1305. parms.port_no = OVSP_LOCAL;
  1306. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1307. ovs_dp_change(dp, a);
  1308. /* So far only local changes have been made, now need the lock. */
  1309. ovs_lock();
  1310. vport = new_vport(&parms);
  1311. if (IS_ERR(vport)) {
  1312. err = PTR_ERR(vport);
  1313. if (err == -EBUSY)
  1314. err = -EEXIST;
  1315. if (err == -EEXIST) {
  1316. /* An outdated user space instance that does not understand
  1317. * the concept of user_features has attempted to create a new
  1318. * datapath and is likely to reuse it. Drop all user features.
  1319. */
  1320. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1321. ovs_dp_reset_user_features(skb, info);
  1322. }
  1323. goto err_destroy_ports_array;
  1324. }
  1325. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1326. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1327. BUG_ON(err < 0);
  1328. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1329. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1330. ovs_unlock();
  1331. ovs_notify(&dp_datapath_genl_family, reply, info);
  1332. return 0;
  1333. err_destroy_ports_array:
  1334. ovs_unlock();
  1335. kfree(dp->ports);
  1336. err_destroy_percpu:
  1337. free_percpu(dp->stats_percpu);
  1338. err_destroy_table:
  1339. ovs_flow_tbl_destroy(&dp->table);
  1340. err_free_dp:
  1341. kfree(dp);
  1342. err_free_reply:
  1343. kfree_skb(reply);
  1344. err:
  1345. return err;
  1346. }
  1347. /* Called with ovs_mutex. */
  1348. static void __dp_destroy(struct datapath *dp)
  1349. {
  1350. int i;
  1351. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1352. struct vport *vport;
  1353. struct hlist_node *n;
  1354. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1355. if (vport->port_no != OVSP_LOCAL)
  1356. ovs_dp_detach_port(vport);
  1357. }
  1358. list_del_rcu(&dp->list_node);
  1359. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1360. * all ports in datapath are destroyed first before freeing datapath.
  1361. */
  1362. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1363. /* RCU destroy the flow table */
  1364. call_rcu(&dp->rcu, destroy_dp_rcu);
  1365. }
  1366. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1367. {
  1368. struct sk_buff *reply;
  1369. struct datapath *dp;
  1370. int err;
  1371. reply = ovs_dp_cmd_alloc_info(info);
  1372. if (!reply)
  1373. return -ENOMEM;
  1374. ovs_lock();
  1375. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1376. err = PTR_ERR(dp);
  1377. if (IS_ERR(dp))
  1378. goto err_unlock_free;
  1379. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1380. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1381. BUG_ON(err < 0);
  1382. __dp_destroy(dp);
  1383. ovs_unlock();
  1384. ovs_notify(&dp_datapath_genl_family, reply, info);
  1385. return 0;
  1386. err_unlock_free:
  1387. ovs_unlock();
  1388. kfree_skb(reply);
  1389. return err;
  1390. }
  1391. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1392. {
  1393. struct sk_buff *reply;
  1394. struct datapath *dp;
  1395. int err;
  1396. reply = ovs_dp_cmd_alloc_info(info);
  1397. if (!reply)
  1398. return -ENOMEM;
  1399. ovs_lock();
  1400. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1401. err = PTR_ERR(dp);
  1402. if (IS_ERR(dp))
  1403. goto err_unlock_free;
  1404. ovs_dp_change(dp, info->attrs);
  1405. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1406. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1407. BUG_ON(err < 0);
  1408. ovs_unlock();
  1409. ovs_notify(&dp_datapath_genl_family, reply, info);
  1410. return 0;
  1411. err_unlock_free:
  1412. ovs_unlock();
  1413. kfree_skb(reply);
  1414. return err;
  1415. }
  1416. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1417. {
  1418. struct sk_buff *reply;
  1419. struct datapath *dp;
  1420. int err;
  1421. reply = ovs_dp_cmd_alloc_info(info);
  1422. if (!reply)
  1423. return -ENOMEM;
  1424. ovs_lock();
  1425. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1426. if (IS_ERR(dp)) {
  1427. err = PTR_ERR(dp);
  1428. goto err_unlock_free;
  1429. }
  1430. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1431. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1432. BUG_ON(err < 0);
  1433. ovs_unlock();
  1434. return genlmsg_reply(reply, info);
  1435. err_unlock_free:
  1436. ovs_unlock();
  1437. kfree_skb(reply);
  1438. return err;
  1439. }
  1440. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1441. {
  1442. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1443. struct datapath *dp;
  1444. int skip = cb->args[0];
  1445. int i = 0;
  1446. ovs_lock();
  1447. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1448. if (i >= skip &&
  1449. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1450. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1451. OVS_DP_CMD_NEW) < 0)
  1452. break;
  1453. i++;
  1454. }
  1455. ovs_unlock();
  1456. cb->args[0] = i;
  1457. return skb->len;
  1458. }
  1459. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1460. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1461. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1462. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1463. };
  1464. static const struct genl_ops dp_datapath_genl_ops[] = {
  1465. { .cmd = OVS_DP_CMD_NEW,
  1466. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1467. .policy = datapath_policy,
  1468. .doit = ovs_dp_cmd_new
  1469. },
  1470. { .cmd = OVS_DP_CMD_DEL,
  1471. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1472. .policy = datapath_policy,
  1473. .doit = ovs_dp_cmd_del
  1474. },
  1475. { .cmd = OVS_DP_CMD_GET,
  1476. .flags = 0, /* OK for unprivileged users. */
  1477. .policy = datapath_policy,
  1478. .doit = ovs_dp_cmd_get,
  1479. .dumpit = ovs_dp_cmd_dump
  1480. },
  1481. { .cmd = OVS_DP_CMD_SET,
  1482. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1483. .policy = datapath_policy,
  1484. .doit = ovs_dp_cmd_set,
  1485. },
  1486. };
  1487. static struct genl_family dp_datapath_genl_family = {
  1488. .id = GENL_ID_GENERATE,
  1489. .hdrsize = sizeof(struct ovs_header),
  1490. .name = OVS_DATAPATH_FAMILY,
  1491. .version = OVS_DATAPATH_VERSION,
  1492. .maxattr = OVS_DP_ATTR_MAX,
  1493. .netnsok = true,
  1494. .parallel_ops = true,
  1495. .ops = dp_datapath_genl_ops,
  1496. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1497. .mcgrps = &ovs_dp_datapath_multicast_group,
  1498. .n_mcgrps = 1,
  1499. };
  1500. /* Called with ovs_mutex or RCU read lock. */
  1501. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1502. u32 portid, u32 seq, u32 flags, u8 cmd)
  1503. {
  1504. struct ovs_header *ovs_header;
  1505. struct ovs_vport_stats vport_stats;
  1506. int err;
  1507. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1508. flags, cmd);
  1509. if (!ovs_header)
  1510. return -EMSGSIZE;
  1511. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1512. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1513. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1514. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1515. vport->ops->get_name(vport)))
  1516. goto nla_put_failure;
  1517. ovs_vport_get_stats(vport, &vport_stats);
  1518. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1519. &vport_stats))
  1520. goto nla_put_failure;
  1521. if (ovs_vport_get_upcall_portids(vport, skb))
  1522. goto nla_put_failure;
  1523. err = ovs_vport_get_options(vport, skb);
  1524. if (err == -EMSGSIZE)
  1525. goto error;
  1526. genlmsg_end(skb, ovs_header);
  1527. return 0;
  1528. nla_put_failure:
  1529. err = -EMSGSIZE;
  1530. error:
  1531. genlmsg_cancel(skb, ovs_header);
  1532. return err;
  1533. }
  1534. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1535. {
  1536. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1537. }
  1538. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1539. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1540. u32 seq, u8 cmd)
  1541. {
  1542. struct sk_buff *skb;
  1543. int retval;
  1544. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1545. if (!skb)
  1546. return ERR_PTR(-ENOMEM);
  1547. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1548. BUG_ON(retval < 0);
  1549. return skb;
  1550. }
  1551. /* Called with ovs_mutex or RCU read lock. */
  1552. static struct vport *lookup_vport(struct net *net,
  1553. const struct ovs_header *ovs_header,
  1554. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1555. {
  1556. struct datapath *dp;
  1557. struct vport *vport;
  1558. if (a[OVS_VPORT_ATTR_NAME]) {
  1559. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1560. if (!vport)
  1561. return ERR_PTR(-ENODEV);
  1562. if (ovs_header->dp_ifindex &&
  1563. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1564. return ERR_PTR(-ENODEV);
  1565. return vport;
  1566. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1567. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1568. if (port_no >= DP_MAX_PORTS)
  1569. return ERR_PTR(-EFBIG);
  1570. dp = get_dp(net, ovs_header->dp_ifindex);
  1571. if (!dp)
  1572. return ERR_PTR(-ENODEV);
  1573. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1574. if (!vport)
  1575. return ERR_PTR(-ENODEV);
  1576. return vport;
  1577. } else
  1578. return ERR_PTR(-EINVAL);
  1579. }
  1580. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1581. {
  1582. struct nlattr **a = info->attrs;
  1583. struct ovs_header *ovs_header = info->userhdr;
  1584. struct vport_parms parms;
  1585. struct sk_buff *reply;
  1586. struct vport *vport;
  1587. struct datapath *dp;
  1588. u32 port_no;
  1589. int err;
  1590. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1591. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1592. return -EINVAL;
  1593. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1594. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1595. if (port_no >= DP_MAX_PORTS)
  1596. return -EFBIG;
  1597. reply = ovs_vport_cmd_alloc_info();
  1598. if (!reply)
  1599. return -ENOMEM;
  1600. ovs_lock();
  1601. restart:
  1602. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1603. err = -ENODEV;
  1604. if (!dp)
  1605. goto exit_unlock_free;
  1606. if (port_no) {
  1607. vport = ovs_vport_ovsl(dp, port_no);
  1608. err = -EBUSY;
  1609. if (vport)
  1610. goto exit_unlock_free;
  1611. } else {
  1612. for (port_no = 1; ; port_no++) {
  1613. if (port_no >= DP_MAX_PORTS) {
  1614. err = -EFBIG;
  1615. goto exit_unlock_free;
  1616. }
  1617. vport = ovs_vport_ovsl(dp, port_no);
  1618. if (!vport)
  1619. break;
  1620. }
  1621. }
  1622. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1623. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1624. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1625. parms.dp = dp;
  1626. parms.port_no = port_no;
  1627. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1628. vport = new_vport(&parms);
  1629. err = PTR_ERR(vport);
  1630. if (IS_ERR(vport)) {
  1631. if (err == -EAGAIN)
  1632. goto restart;
  1633. goto exit_unlock_free;
  1634. }
  1635. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1636. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1637. BUG_ON(err < 0);
  1638. ovs_unlock();
  1639. ovs_notify(&dp_vport_genl_family, reply, info);
  1640. return 0;
  1641. exit_unlock_free:
  1642. ovs_unlock();
  1643. kfree_skb(reply);
  1644. return err;
  1645. }
  1646. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1647. {
  1648. struct nlattr **a = info->attrs;
  1649. struct sk_buff *reply;
  1650. struct vport *vport;
  1651. int err;
  1652. reply = ovs_vport_cmd_alloc_info();
  1653. if (!reply)
  1654. return -ENOMEM;
  1655. ovs_lock();
  1656. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1657. err = PTR_ERR(vport);
  1658. if (IS_ERR(vport))
  1659. goto exit_unlock_free;
  1660. if (a[OVS_VPORT_ATTR_TYPE] &&
  1661. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1662. err = -EINVAL;
  1663. goto exit_unlock_free;
  1664. }
  1665. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1666. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1667. if (err)
  1668. goto exit_unlock_free;
  1669. }
  1670. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1671. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1672. err = ovs_vport_set_upcall_portids(vport, ids);
  1673. if (err)
  1674. goto exit_unlock_free;
  1675. }
  1676. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1677. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1678. BUG_ON(err < 0);
  1679. ovs_unlock();
  1680. ovs_notify(&dp_vport_genl_family, reply, info);
  1681. return 0;
  1682. exit_unlock_free:
  1683. ovs_unlock();
  1684. kfree_skb(reply);
  1685. return err;
  1686. }
  1687. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1688. {
  1689. struct nlattr **a = info->attrs;
  1690. struct sk_buff *reply;
  1691. struct vport *vport;
  1692. int err;
  1693. reply = ovs_vport_cmd_alloc_info();
  1694. if (!reply)
  1695. return -ENOMEM;
  1696. ovs_lock();
  1697. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1698. err = PTR_ERR(vport);
  1699. if (IS_ERR(vport))
  1700. goto exit_unlock_free;
  1701. if (vport->port_no == OVSP_LOCAL) {
  1702. err = -EINVAL;
  1703. goto exit_unlock_free;
  1704. }
  1705. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1706. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1707. BUG_ON(err < 0);
  1708. ovs_dp_detach_port(vport);
  1709. ovs_unlock();
  1710. ovs_notify(&dp_vport_genl_family, reply, info);
  1711. return 0;
  1712. exit_unlock_free:
  1713. ovs_unlock();
  1714. kfree_skb(reply);
  1715. return err;
  1716. }
  1717. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1718. {
  1719. struct nlattr **a = info->attrs;
  1720. struct ovs_header *ovs_header = info->userhdr;
  1721. struct sk_buff *reply;
  1722. struct vport *vport;
  1723. int err;
  1724. reply = ovs_vport_cmd_alloc_info();
  1725. if (!reply)
  1726. return -ENOMEM;
  1727. rcu_read_lock();
  1728. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1729. err = PTR_ERR(vport);
  1730. if (IS_ERR(vport))
  1731. goto exit_unlock_free;
  1732. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1733. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1734. BUG_ON(err < 0);
  1735. rcu_read_unlock();
  1736. return genlmsg_reply(reply, info);
  1737. exit_unlock_free:
  1738. rcu_read_unlock();
  1739. kfree_skb(reply);
  1740. return err;
  1741. }
  1742. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1743. {
  1744. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1745. struct datapath *dp;
  1746. int bucket = cb->args[0], skip = cb->args[1];
  1747. int i, j = 0;
  1748. rcu_read_lock();
  1749. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1750. if (!dp) {
  1751. rcu_read_unlock();
  1752. return -ENODEV;
  1753. }
  1754. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1755. struct vport *vport;
  1756. j = 0;
  1757. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1758. if (j >= skip &&
  1759. ovs_vport_cmd_fill_info(vport, skb,
  1760. NETLINK_CB(cb->skb).portid,
  1761. cb->nlh->nlmsg_seq,
  1762. NLM_F_MULTI,
  1763. OVS_VPORT_CMD_NEW) < 0)
  1764. goto out;
  1765. j++;
  1766. }
  1767. skip = 0;
  1768. }
  1769. out:
  1770. rcu_read_unlock();
  1771. cb->args[0] = i;
  1772. cb->args[1] = j;
  1773. return skb->len;
  1774. }
  1775. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1776. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1777. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1778. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1779. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1780. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1781. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1782. };
  1783. static const struct genl_ops dp_vport_genl_ops[] = {
  1784. { .cmd = OVS_VPORT_CMD_NEW,
  1785. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1786. .policy = vport_policy,
  1787. .doit = ovs_vport_cmd_new
  1788. },
  1789. { .cmd = OVS_VPORT_CMD_DEL,
  1790. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1791. .policy = vport_policy,
  1792. .doit = ovs_vport_cmd_del
  1793. },
  1794. { .cmd = OVS_VPORT_CMD_GET,
  1795. .flags = 0, /* OK for unprivileged users. */
  1796. .policy = vport_policy,
  1797. .doit = ovs_vport_cmd_get,
  1798. .dumpit = ovs_vport_cmd_dump
  1799. },
  1800. { .cmd = OVS_VPORT_CMD_SET,
  1801. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1802. .policy = vport_policy,
  1803. .doit = ovs_vport_cmd_set,
  1804. },
  1805. };
  1806. struct genl_family dp_vport_genl_family = {
  1807. .id = GENL_ID_GENERATE,
  1808. .hdrsize = sizeof(struct ovs_header),
  1809. .name = OVS_VPORT_FAMILY,
  1810. .version = OVS_VPORT_VERSION,
  1811. .maxattr = OVS_VPORT_ATTR_MAX,
  1812. .netnsok = true,
  1813. .parallel_ops = true,
  1814. .ops = dp_vport_genl_ops,
  1815. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1816. .mcgrps = &ovs_dp_vport_multicast_group,
  1817. .n_mcgrps = 1,
  1818. };
  1819. static struct genl_family * const dp_genl_families[] = {
  1820. &dp_datapath_genl_family,
  1821. &dp_vport_genl_family,
  1822. &dp_flow_genl_family,
  1823. &dp_packet_genl_family,
  1824. };
  1825. static void dp_unregister_genl(int n_families)
  1826. {
  1827. int i;
  1828. for (i = 0; i < n_families; i++)
  1829. genl_unregister_family(dp_genl_families[i]);
  1830. }
  1831. static int dp_register_genl(void)
  1832. {
  1833. int err;
  1834. int i;
  1835. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1836. err = genl_register_family(dp_genl_families[i]);
  1837. if (err)
  1838. goto error;
  1839. }
  1840. return 0;
  1841. error:
  1842. dp_unregister_genl(i);
  1843. return err;
  1844. }
  1845. static int __net_init ovs_init_net(struct net *net)
  1846. {
  1847. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1848. INIT_LIST_HEAD(&ovs_net->dps);
  1849. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1850. return 0;
  1851. }
  1852. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1853. struct list_head *head)
  1854. {
  1855. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1856. struct datapath *dp;
  1857. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1858. int i;
  1859. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1860. struct vport *vport;
  1861. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1862. struct netdev_vport *netdev_vport;
  1863. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1864. continue;
  1865. netdev_vport = netdev_vport_priv(vport);
  1866. if (dev_net(netdev_vport->dev) == dnet)
  1867. list_add(&vport->detach_list, head);
  1868. }
  1869. }
  1870. }
  1871. }
  1872. static void __net_exit ovs_exit_net(struct net *dnet)
  1873. {
  1874. struct datapath *dp, *dp_next;
  1875. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1876. struct vport *vport, *vport_next;
  1877. struct net *net;
  1878. LIST_HEAD(head);
  1879. ovs_lock();
  1880. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1881. __dp_destroy(dp);
  1882. rtnl_lock();
  1883. for_each_net(net)
  1884. list_vports_from_net(net, dnet, &head);
  1885. rtnl_unlock();
  1886. /* Detach all vports from given namespace. */
  1887. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1888. list_del(&vport->detach_list);
  1889. ovs_dp_detach_port(vport);
  1890. }
  1891. ovs_unlock();
  1892. cancel_work_sync(&ovs_net->dp_notify_work);
  1893. }
  1894. static struct pernet_operations ovs_net_ops = {
  1895. .init = ovs_init_net,
  1896. .exit = ovs_exit_net,
  1897. .id = &ovs_net_id,
  1898. .size = sizeof(struct ovs_net),
  1899. };
  1900. static int __init dp_init(void)
  1901. {
  1902. int err;
  1903. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1904. pr_info("Open vSwitch switching datapath\n");
  1905. err = action_fifos_init();
  1906. if (err)
  1907. goto error;
  1908. err = ovs_internal_dev_rtnl_link_register();
  1909. if (err)
  1910. goto error_action_fifos_exit;
  1911. err = ovs_flow_init();
  1912. if (err)
  1913. goto error_unreg_rtnl_link;
  1914. err = ovs_vport_init();
  1915. if (err)
  1916. goto error_flow_exit;
  1917. err = register_pernet_device(&ovs_net_ops);
  1918. if (err)
  1919. goto error_vport_exit;
  1920. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1921. if (err)
  1922. goto error_netns_exit;
  1923. err = ovs_netdev_init();
  1924. if (err)
  1925. goto error_unreg_notifier;
  1926. err = dp_register_genl();
  1927. if (err < 0)
  1928. goto error_unreg_netdev;
  1929. return 0;
  1930. error_unreg_netdev:
  1931. ovs_netdev_exit();
  1932. error_unreg_notifier:
  1933. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1934. error_netns_exit:
  1935. unregister_pernet_device(&ovs_net_ops);
  1936. error_vport_exit:
  1937. ovs_vport_exit();
  1938. error_flow_exit:
  1939. ovs_flow_exit();
  1940. error_unreg_rtnl_link:
  1941. ovs_internal_dev_rtnl_link_unregister();
  1942. error_action_fifos_exit:
  1943. action_fifos_exit();
  1944. error:
  1945. return err;
  1946. }
  1947. static void dp_cleanup(void)
  1948. {
  1949. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1950. ovs_netdev_exit();
  1951. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1952. unregister_pernet_device(&ovs_net_ops);
  1953. rcu_barrier();
  1954. ovs_vport_exit();
  1955. ovs_flow_exit();
  1956. ovs_internal_dev_rtnl_link_unregister();
  1957. action_fifos_exit();
  1958. }
  1959. module_init(dp_init);
  1960. module_exit(dp_cleanup);
  1961. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1962. MODULE_LICENSE("GPL");