datapath.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. /*
  2. * Copyright (c) 2007-2012 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 <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. #include "datapath.h"
  53. #include "flow.h"
  54. #include "vport-internal_dev.h"
  55. /**
  56. * struct ovs_net - Per net-namespace data for ovs.
  57. * @dps: List of datapaths to enable dumping them all out.
  58. * Protected by genl_mutex.
  59. */
  60. struct ovs_net {
  61. struct list_head dps;
  62. };
  63. static int ovs_net_id __read_mostly;
  64. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  65. static void rehash_flow_table(struct work_struct *work);
  66. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  67. /**
  68. * DOC: Locking:
  69. *
  70. * Writes to device state (add/remove datapath, port, set operations on vports,
  71. * etc.) are protected by RTNL.
  72. *
  73. * Writes to other state (flow table modifications, set miscellaneous datapath
  74. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  75. * genl_mutex.
  76. *
  77. * Reads are protected by RCU.
  78. *
  79. * There are a few special cases (mostly stats) that have their own
  80. * synchronization but they nest under all of above and don't interact with
  81. * each other.
  82. */
  83. static struct vport *new_vport(const struct vport_parms *);
  84. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  85. const struct dp_upcall_info *);
  86. static int queue_userspace_packet(struct net *, int dp_ifindex,
  87. struct sk_buff *,
  88. const struct dp_upcall_info *);
  89. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  90. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  91. {
  92. struct datapath *dp = NULL;
  93. struct net_device *dev;
  94. rcu_read_lock();
  95. dev = dev_get_by_index_rcu(net, dp_ifindex);
  96. if (dev) {
  97. struct vport *vport = ovs_internal_dev_get_vport(dev);
  98. if (vport)
  99. dp = vport->dp;
  100. }
  101. rcu_read_unlock();
  102. return dp;
  103. }
  104. /* Must be called with rcu_read_lock or RTNL lock. */
  105. const char *ovs_dp_name(const struct datapath *dp)
  106. {
  107. struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
  108. return vport->ops->get_name(vport);
  109. }
  110. static int get_dpifindex(struct datapath *dp)
  111. {
  112. struct vport *local;
  113. int ifindex;
  114. rcu_read_lock();
  115. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  116. if (local)
  117. ifindex = local->ops->get_ifindex(local);
  118. else
  119. ifindex = 0;
  120. rcu_read_unlock();
  121. return ifindex;
  122. }
  123. static void destroy_dp_rcu(struct rcu_head *rcu)
  124. {
  125. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  126. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  127. free_percpu(dp->stats_percpu);
  128. release_net(ovs_dp_get_net(dp));
  129. kfree(dp->ports);
  130. kfree(dp);
  131. }
  132. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  133. u16 port_no)
  134. {
  135. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  136. }
  137. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  138. {
  139. struct vport *vport;
  140. struct hlist_head *head;
  141. head = vport_hash_bucket(dp, port_no);
  142. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  143. if (vport->port_no == port_no)
  144. return vport;
  145. }
  146. return NULL;
  147. }
  148. /* Called with RTNL lock and genl_lock. */
  149. static struct vport *new_vport(const struct vport_parms *parms)
  150. {
  151. struct vport *vport;
  152. vport = ovs_vport_add(parms);
  153. if (!IS_ERR(vport)) {
  154. struct datapath *dp = parms->dp;
  155. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  156. hlist_add_head_rcu(&vport->dp_hash_node, head);
  157. }
  158. return vport;
  159. }
  160. /* Called with RTNL lock. */
  161. void ovs_dp_detach_port(struct vport *p)
  162. {
  163. ASSERT_RTNL();
  164. /* First drop references to device. */
  165. hlist_del_rcu(&p->dp_hash_node);
  166. /* Then destroy it. */
  167. ovs_vport_del(p);
  168. }
  169. /* Must be called with rcu_read_lock. */
  170. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  171. {
  172. struct datapath *dp = p->dp;
  173. struct sw_flow *flow;
  174. struct dp_stats_percpu *stats;
  175. struct sw_flow_key key;
  176. u64 *stats_counter;
  177. int error;
  178. int key_len;
  179. stats = this_cpu_ptr(dp->stats_percpu);
  180. /* Extract flow from 'skb' into 'key'. */
  181. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  182. if (unlikely(error)) {
  183. kfree_skb(skb);
  184. return;
  185. }
  186. /* Look up flow. */
  187. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  188. if (unlikely(!flow)) {
  189. struct dp_upcall_info upcall;
  190. upcall.cmd = OVS_PACKET_CMD_MISS;
  191. upcall.key = &key;
  192. upcall.userdata = NULL;
  193. upcall.portid = p->upcall_portid;
  194. ovs_dp_upcall(dp, skb, &upcall);
  195. consume_skb(skb);
  196. stats_counter = &stats->n_missed;
  197. goto out;
  198. }
  199. OVS_CB(skb)->flow = flow;
  200. stats_counter = &stats->n_hit;
  201. ovs_flow_used(OVS_CB(skb)->flow, skb);
  202. ovs_execute_actions(dp, skb);
  203. out:
  204. /* Update datapath statistics. */
  205. u64_stats_update_begin(&stats->sync);
  206. (*stats_counter)++;
  207. u64_stats_update_end(&stats->sync);
  208. }
  209. static struct genl_family dp_packet_genl_family = {
  210. .id = GENL_ID_GENERATE,
  211. .hdrsize = sizeof(struct ovs_header),
  212. .name = OVS_PACKET_FAMILY,
  213. .version = OVS_PACKET_VERSION,
  214. .maxattr = OVS_PACKET_ATTR_MAX,
  215. .netnsok = true
  216. };
  217. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  218. const struct dp_upcall_info *upcall_info)
  219. {
  220. struct dp_stats_percpu *stats;
  221. int dp_ifindex;
  222. int err;
  223. if (upcall_info->portid == 0) {
  224. err = -ENOTCONN;
  225. goto err;
  226. }
  227. dp_ifindex = get_dpifindex(dp);
  228. if (!dp_ifindex) {
  229. err = -ENODEV;
  230. goto err;
  231. }
  232. if (!skb_is_gso(skb))
  233. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  234. else
  235. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  236. if (err)
  237. goto err;
  238. return 0;
  239. err:
  240. stats = this_cpu_ptr(dp->stats_percpu);
  241. u64_stats_update_begin(&stats->sync);
  242. stats->n_lost++;
  243. u64_stats_update_end(&stats->sync);
  244. return err;
  245. }
  246. static int queue_gso_packets(struct net *net, int dp_ifindex,
  247. struct sk_buff *skb,
  248. const struct dp_upcall_info *upcall_info)
  249. {
  250. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  251. struct dp_upcall_info later_info;
  252. struct sw_flow_key later_key;
  253. struct sk_buff *segs, *nskb;
  254. int err;
  255. segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
  256. if (IS_ERR(segs))
  257. return PTR_ERR(segs);
  258. /* Queue all of the segments. */
  259. skb = segs;
  260. do {
  261. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  262. if (err)
  263. break;
  264. if (skb == segs && gso_type & SKB_GSO_UDP) {
  265. /* The initial flow key extracted by ovs_flow_extract()
  266. * in this case is for a first fragment, so we need to
  267. * properly mark later fragments.
  268. */
  269. later_key = *upcall_info->key;
  270. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  271. later_info = *upcall_info;
  272. later_info.key = &later_key;
  273. upcall_info = &later_info;
  274. }
  275. } while ((skb = skb->next));
  276. /* Free all of the segments. */
  277. skb = segs;
  278. do {
  279. nskb = skb->next;
  280. if (err)
  281. kfree_skb(skb);
  282. else
  283. consume_skb(skb);
  284. } while ((skb = nskb));
  285. return err;
  286. }
  287. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  288. struct sk_buff *skb,
  289. const struct dp_upcall_info *upcall_info)
  290. {
  291. struct ovs_header *upcall;
  292. struct sk_buff *nskb = NULL;
  293. struct sk_buff *user_skb; /* to be queued to userspace */
  294. struct nlattr *nla;
  295. unsigned int len;
  296. int err;
  297. if (vlan_tx_tag_present(skb)) {
  298. nskb = skb_clone(skb, GFP_ATOMIC);
  299. if (!nskb)
  300. return -ENOMEM;
  301. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  302. if (!nskb)
  303. return -ENOMEM;
  304. nskb->vlan_tci = 0;
  305. skb = nskb;
  306. }
  307. if (nla_attr_size(skb->len) > USHRT_MAX) {
  308. err = -EFBIG;
  309. goto out;
  310. }
  311. len = sizeof(struct ovs_header);
  312. len += nla_total_size(skb->len);
  313. len += nla_total_size(FLOW_BUFSIZE);
  314. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  315. len += nla_total_size(8);
  316. user_skb = genlmsg_new(len, GFP_ATOMIC);
  317. if (!user_skb) {
  318. err = -ENOMEM;
  319. goto out;
  320. }
  321. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  322. 0, upcall_info->cmd);
  323. upcall->dp_ifindex = dp_ifindex;
  324. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  325. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  326. nla_nest_end(user_skb, nla);
  327. if (upcall_info->userdata)
  328. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  329. nla_get_u64(upcall_info->userdata));
  330. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  331. skb_copy_and_csum_dev(skb, nla_data(nla));
  332. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  333. out:
  334. kfree_skb(nskb);
  335. return err;
  336. }
  337. /* Called with genl_mutex. */
  338. static int flush_flows(struct datapath *dp)
  339. {
  340. struct flow_table *old_table;
  341. struct flow_table *new_table;
  342. old_table = genl_dereference(dp->table);
  343. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  344. if (!new_table)
  345. return -ENOMEM;
  346. rcu_assign_pointer(dp->table, new_table);
  347. ovs_flow_tbl_deferred_destroy(old_table);
  348. return 0;
  349. }
  350. static int validate_actions(const struct nlattr *attr,
  351. const struct sw_flow_key *key, int depth);
  352. static int validate_sample(const struct nlattr *attr,
  353. const struct sw_flow_key *key, int depth)
  354. {
  355. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  356. const struct nlattr *probability, *actions;
  357. const struct nlattr *a;
  358. int rem;
  359. memset(attrs, 0, sizeof(attrs));
  360. nla_for_each_nested(a, attr, rem) {
  361. int type = nla_type(a);
  362. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  363. return -EINVAL;
  364. attrs[type] = a;
  365. }
  366. if (rem)
  367. return -EINVAL;
  368. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  369. if (!probability || nla_len(probability) != sizeof(u32))
  370. return -EINVAL;
  371. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  372. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  373. return -EINVAL;
  374. return validate_actions(actions, key, depth + 1);
  375. }
  376. static int validate_tp_port(const struct sw_flow_key *flow_key)
  377. {
  378. if (flow_key->eth.type == htons(ETH_P_IP)) {
  379. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  380. return 0;
  381. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  382. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  383. return 0;
  384. }
  385. return -EINVAL;
  386. }
  387. static int validate_set(const struct nlattr *a,
  388. const struct sw_flow_key *flow_key)
  389. {
  390. const struct nlattr *ovs_key = nla_data(a);
  391. int key_type = nla_type(ovs_key);
  392. /* There can be only one key in a action */
  393. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  394. return -EINVAL;
  395. if (key_type > OVS_KEY_ATTR_MAX ||
  396. nla_len(ovs_key) != ovs_key_lens[key_type])
  397. return -EINVAL;
  398. switch (key_type) {
  399. const struct ovs_key_ipv4 *ipv4_key;
  400. const struct ovs_key_ipv6 *ipv6_key;
  401. case OVS_KEY_ATTR_PRIORITY:
  402. case OVS_KEY_ATTR_SKB_MARK:
  403. case OVS_KEY_ATTR_ETHERNET:
  404. break;
  405. case OVS_KEY_ATTR_IPV4:
  406. if (flow_key->eth.type != htons(ETH_P_IP))
  407. return -EINVAL;
  408. if (!flow_key->ip.proto)
  409. return -EINVAL;
  410. ipv4_key = nla_data(ovs_key);
  411. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  412. return -EINVAL;
  413. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  414. return -EINVAL;
  415. break;
  416. case OVS_KEY_ATTR_IPV6:
  417. if (flow_key->eth.type != htons(ETH_P_IPV6))
  418. return -EINVAL;
  419. if (!flow_key->ip.proto)
  420. return -EINVAL;
  421. ipv6_key = nla_data(ovs_key);
  422. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  423. return -EINVAL;
  424. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  425. return -EINVAL;
  426. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  427. return -EINVAL;
  428. break;
  429. case OVS_KEY_ATTR_TCP:
  430. if (flow_key->ip.proto != IPPROTO_TCP)
  431. return -EINVAL;
  432. return validate_tp_port(flow_key);
  433. case OVS_KEY_ATTR_UDP:
  434. if (flow_key->ip.proto != IPPROTO_UDP)
  435. return -EINVAL;
  436. return validate_tp_port(flow_key);
  437. default:
  438. return -EINVAL;
  439. }
  440. return 0;
  441. }
  442. static int validate_userspace(const struct nlattr *attr)
  443. {
  444. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  445. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  446. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  447. };
  448. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  449. int error;
  450. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  451. attr, userspace_policy);
  452. if (error)
  453. return error;
  454. if (!a[OVS_USERSPACE_ATTR_PID] ||
  455. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  456. return -EINVAL;
  457. return 0;
  458. }
  459. static int validate_actions(const struct nlattr *attr,
  460. const struct sw_flow_key *key, int depth)
  461. {
  462. const struct nlattr *a;
  463. int rem, err;
  464. if (depth >= SAMPLE_ACTION_DEPTH)
  465. return -EOVERFLOW;
  466. nla_for_each_nested(a, attr, rem) {
  467. /* Expected argument lengths, (u32)-1 for variable length. */
  468. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  469. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  470. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  471. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  472. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  473. [OVS_ACTION_ATTR_SET] = (u32)-1,
  474. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  475. };
  476. const struct ovs_action_push_vlan *vlan;
  477. int type = nla_type(a);
  478. if (type > OVS_ACTION_ATTR_MAX ||
  479. (action_lens[type] != nla_len(a) &&
  480. action_lens[type] != (u32)-1))
  481. return -EINVAL;
  482. switch (type) {
  483. case OVS_ACTION_ATTR_UNSPEC:
  484. return -EINVAL;
  485. case OVS_ACTION_ATTR_USERSPACE:
  486. err = validate_userspace(a);
  487. if (err)
  488. return err;
  489. break;
  490. case OVS_ACTION_ATTR_OUTPUT:
  491. if (nla_get_u32(a) >= DP_MAX_PORTS)
  492. return -EINVAL;
  493. break;
  494. case OVS_ACTION_ATTR_POP_VLAN:
  495. break;
  496. case OVS_ACTION_ATTR_PUSH_VLAN:
  497. vlan = nla_data(a);
  498. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  499. return -EINVAL;
  500. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  501. return -EINVAL;
  502. break;
  503. case OVS_ACTION_ATTR_SET:
  504. err = validate_set(a, key);
  505. if (err)
  506. return err;
  507. break;
  508. case OVS_ACTION_ATTR_SAMPLE:
  509. err = validate_sample(a, key, depth);
  510. if (err)
  511. return err;
  512. break;
  513. default:
  514. return -EINVAL;
  515. }
  516. }
  517. if (rem > 0)
  518. return -EINVAL;
  519. return 0;
  520. }
  521. static void clear_stats(struct sw_flow *flow)
  522. {
  523. flow->used = 0;
  524. flow->tcp_flags = 0;
  525. flow->packet_count = 0;
  526. flow->byte_count = 0;
  527. }
  528. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  529. {
  530. struct ovs_header *ovs_header = info->userhdr;
  531. struct nlattr **a = info->attrs;
  532. struct sw_flow_actions *acts;
  533. struct sk_buff *packet;
  534. struct sw_flow *flow;
  535. struct datapath *dp;
  536. struct ethhdr *eth;
  537. int len;
  538. int err;
  539. int key_len;
  540. err = -EINVAL;
  541. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  542. !a[OVS_PACKET_ATTR_ACTIONS] ||
  543. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  544. goto err;
  545. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  546. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  547. err = -ENOMEM;
  548. if (!packet)
  549. goto err;
  550. skb_reserve(packet, NET_IP_ALIGN);
  551. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  552. skb_reset_mac_header(packet);
  553. eth = eth_hdr(packet);
  554. /* Normally, setting the skb 'protocol' field would be handled by a
  555. * call to eth_type_trans(), but it assumes there's a sending
  556. * device, which we may not have. */
  557. if (ntohs(eth->h_proto) >= 1536)
  558. packet->protocol = eth->h_proto;
  559. else
  560. packet->protocol = htons(ETH_P_802_2);
  561. /* Build an sw_flow for sending this packet. */
  562. flow = ovs_flow_alloc();
  563. err = PTR_ERR(flow);
  564. if (IS_ERR(flow))
  565. goto err_kfree_skb;
  566. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  567. if (err)
  568. goto err_flow_free;
  569. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  570. &flow->key.phy.skb_mark,
  571. &flow->key.phy.in_port,
  572. a[OVS_PACKET_ATTR_KEY]);
  573. if (err)
  574. goto err_flow_free;
  575. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  576. if (err)
  577. goto err_flow_free;
  578. flow->hash = ovs_flow_hash(&flow->key, key_len);
  579. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  580. err = PTR_ERR(acts);
  581. if (IS_ERR(acts))
  582. goto err_flow_free;
  583. rcu_assign_pointer(flow->sf_acts, acts);
  584. OVS_CB(packet)->flow = flow;
  585. packet->priority = flow->key.phy.priority;
  586. packet->mark = flow->key.phy.skb_mark;
  587. rcu_read_lock();
  588. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  589. err = -ENODEV;
  590. if (!dp)
  591. goto err_unlock;
  592. local_bh_disable();
  593. err = ovs_execute_actions(dp, packet);
  594. local_bh_enable();
  595. rcu_read_unlock();
  596. ovs_flow_free(flow);
  597. return err;
  598. err_unlock:
  599. rcu_read_unlock();
  600. err_flow_free:
  601. ovs_flow_free(flow);
  602. err_kfree_skb:
  603. kfree_skb(packet);
  604. err:
  605. return err;
  606. }
  607. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  608. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  609. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  610. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  611. };
  612. static struct genl_ops dp_packet_genl_ops[] = {
  613. { .cmd = OVS_PACKET_CMD_EXECUTE,
  614. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  615. .policy = packet_policy,
  616. .doit = ovs_packet_cmd_execute
  617. }
  618. };
  619. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  620. {
  621. int i;
  622. struct flow_table *table = genl_dereference(dp->table);
  623. stats->n_flows = ovs_flow_tbl_count(table);
  624. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  625. for_each_possible_cpu(i) {
  626. const struct dp_stats_percpu *percpu_stats;
  627. struct dp_stats_percpu local_stats;
  628. unsigned int start;
  629. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  630. do {
  631. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  632. local_stats = *percpu_stats;
  633. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  634. stats->n_hit += local_stats.n_hit;
  635. stats->n_missed += local_stats.n_missed;
  636. stats->n_lost += local_stats.n_lost;
  637. }
  638. }
  639. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  640. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  641. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  642. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  643. };
  644. static struct genl_family dp_flow_genl_family = {
  645. .id = GENL_ID_GENERATE,
  646. .hdrsize = sizeof(struct ovs_header),
  647. .name = OVS_FLOW_FAMILY,
  648. .version = OVS_FLOW_VERSION,
  649. .maxattr = OVS_FLOW_ATTR_MAX,
  650. .netnsok = true
  651. };
  652. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  653. .name = OVS_FLOW_MCGROUP
  654. };
  655. /* Called with genl_lock. */
  656. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  657. struct sk_buff *skb, u32 portid,
  658. u32 seq, u32 flags, u8 cmd)
  659. {
  660. const int skb_orig_len = skb->len;
  661. const struct sw_flow_actions *sf_acts;
  662. struct ovs_flow_stats stats;
  663. struct ovs_header *ovs_header;
  664. struct nlattr *nla;
  665. unsigned long used;
  666. u8 tcp_flags;
  667. int err;
  668. sf_acts = rcu_dereference_protected(flow->sf_acts,
  669. lockdep_genl_is_held());
  670. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  671. if (!ovs_header)
  672. return -EMSGSIZE;
  673. ovs_header->dp_ifindex = get_dpifindex(dp);
  674. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  675. if (!nla)
  676. goto nla_put_failure;
  677. err = ovs_flow_to_nlattrs(&flow->key, skb);
  678. if (err)
  679. goto error;
  680. nla_nest_end(skb, nla);
  681. spin_lock_bh(&flow->lock);
  682. used = flow->used;
  683. stats.n_packets = flow->packet_count;
  684. stats.n_bytes = flow->byte_count;
  685. tcp_flags = flow->tcp_flags;
  686. spin_unlock_bh(&flow->lock);
  687. if (used &&
  688. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  689. goto nla_put_failure;
  690. if (stats.n_packets &&
  691. nla_put(skb, OVS_FLOW_ATTR_STATS,
  692. sizeof(struct ovs_flow_stats), &stats))
  693. goto nla_put_failure;
  694. if (tcp_flags &&
  695. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  696. goto nla_put_failure;
  697. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  698. * this is the first flow to be dumped into 'skb'. This is unusual for
  699. * Netlink but individual action lists can be longer than
  700. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  701. * The userspace caller can always fetch the actions separately if it
  702. * really wants them. (Most userspace callers in fact don't care.)
  703. *
  704. * This can only fail for dump operations because the skb is always
  705. * properly sized for single flows.
  706. */
  707. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  708. sf_acts->actions);
  709. if (err < 0 && skb_orig_len)
  710. goto error;
  711. return genlmsg_end(skb, ovs_header);
  712. nla_put_failure:
  713. err = -EMSGSIZE;
  714. error:
  715. genlmsg_cancel(skb, ovs_header);
  716. return err;
  717. }
  718. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  719. {
  720. const struct sw_flow_actions *sf_acts;
  721. int len;
  722. sf_acts = rcu_dereference_protected(flow->sf_acts,
  723. lockdep_genl_is_held());
  724. /* OVS_FLOW_ATTR_KEY */
  725. len = nla_total_size(FLOW_BUFSIZE);
  726. /* OVS_FLOW_ATTR_ACTIONS */
  727. len += nla_total_size(sf_acts->actions_len);
  728. /* OVS_FLOW_ATTR_STATS */
  729. len += nla_total_size(sizeof(struct ovs_flow_stats));
  730. /* OVS_FLOW_ATTR_TCP_FLAGS */
  731. len += nla_total_size(1);
  732. /* OVS_FLOW_ATTR_USED */
  733. len += nla_total_size(8);
  734. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  735. return genlmsg_new(len, GFP_KERNEL);
  736. }
  737. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  738. struct datapath *dp,
  739. u32 portid, u32 seq, u8 cmd)
  740. {
  741. struct sk_buff *skb;
  742. int retval;
  743. skb = ovs_flow_cmd_alloc_info(flow);
  744. if (!skb)
  745. return ERR_PTR(-ENOMEM);
  746. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  747. BUG_ON(retval < 0);
  748. return skb;
  749. }
  750. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  751. {
  752. struct nlattr **a = info->attrs;
  753. struct ovs_header *ovs_header = info->userhdr;
  754. struct sw_flow_key key;
  755. struct sw_flow *flow;
  756. struct sk_buff *reply;
  757. struct datapath *dp;
  758. struct flow_table *table;
  759. int error;
  760. int key_len;
  761. /* Extract key. */
  762. error = -EINVAL;
  763. if (!a[OVS_FLOW_ATTR_KEY])
  764. goto error;
  765. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  766. if (error)
  767. goto error;
  768. /* Validate actions. */
  769. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  770. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  771. if (error)
  772. goto error;
  773. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  774. error = -EINVAL;
  775. goto error;
  776. }
  777. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  778. error = -ENODEV;
  779. if (!dp)
  780. goto error;
  781. table = genl_dereference(dp->table);
  782. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  783. if (!flow) {
  784. struct sw_flow_actions *acts;
  785. /* Bail out if we're not allowed to create a new flow. */
  786. error = -ENOENT;
  787. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  788. goto error;
  789. /* Expand table, if necessary, to make room. */
  790. if (ovs_flow_tbl_need_to_expand(table)) {
  791. struct flow_table *new_table;
  792. new_table = ovs_flow_tbl_expand(table);
  793. if (!IS_ERR(new_table)) {
  794. rcu_assign_pointer(dp->table, new_table);
  795. ovs_flow_tbl_deferred_destroy(table);
  796. table = genl_dereference(dp->table);
  797. }
  798. }
  799. /* Allocate flow. */
  800. flow = ovs_flow_alloc();
  801. if (IS_ERR(flow)) {
  802. error = PTR_ERR(flow);
  803. goto error;
  804. }
  805. flow->key = key;
  806. clear_stats(flow);
  807. /* Obtain actions. */
  808. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  809. error = PTR_ERR(acts);
  810. if (IS_ERR(acts))
  811. goto error_free_flow;
  812. rcu_assign_pointer(flow->sf_acts, acts);
  813. /* Put flow in bucket. */
  814. flow->hash = ovs_flow_hash(&key, key_len);
  815. ovs_flow_tbl_insert(table, flow);
  816. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  817. info->snd_seq,
  818. OVS_FLOW_CMD_NEW);
  819. } else {
  820. /* We found a matching flow. */
  821. struct sw_flow_actions *old_acts;
  822. struct nlattr *acts_attrs;
  823. /* Bail out if we're not allowed to modify an existing flow.
  824. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  825. * because Generic Netlink treats the latter as a dump
  826. * request. We also accept NLM_F_EXCL in case that bug ever
  827. * gets fixed.
  828. */
  829. error = -EEXIST;
  830. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  831. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  832. goto error;
  833. /* Update actions. */
  834. old_acts = rcu_dereference_protected(flow->sf_acts,
  835. lockdep_genl_is_held());
  836. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  837. if (acts_attrs &&
  838. (old_acts->actions_len != nla_len(acts_attrs) ||
  839. memcmp(old_acts->actions, nla_data(acts_attrs),
  840. old_acts->actions_len))) {
  841. struct sw_flow_actions *new_acts;
  842. new_acts = ovs_flow_actions_alloc(acts_attrs);
  843. error = PTR_ERR(new_acts);
  844. if (IS_ERR(new_acts))
  845. goto error;
  846. rcu_assign_pointer(flow->sf_acts, new_acts);
  847. ovs_flow_deferred_free_acts(old_acts);
  848. }
  849. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  850. info->snd_seq, OVS_FLOW_CMD_NEW);
  851. /* Clear stats. */
  852. if (a[OVS_FLOW_ATTR_CLEAR]) {
  853. spin_lock_bh(&flow->lock);
  854. clear_stats(flow);
  855. spin_unlock_bh(&flow->lock);
  856. }
  857. }
  858. if (!IS_ERR(reply))
  859. genl_notify(reply, genl_info_net(info), info->snd_portid,
  860. ovs_dp_flow_multicast_group.id, info->nlhdr,
  861. GFP_KERNEL);
  862. else
  863. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  864. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  865. return 0;
  866. error_free_flow:
  867. ovs_flow_free(flow);
  868. error:
  869. return error;
  870. }
  871. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  872. {
  873. struct nlattr **a = info->attrs;
  874. struct ovs_header *ovs_header = info->userhdr;
  875. struct sw_flow_key key;
  876. struct sk_buff *reply;
  877. struct sw_flow *flow;
  878. struct datapath *dp;
  879. struct flow_table *table;
  880. int err;
  881. int key_len;
  882. if (!a[OVS_FLOW_ATTR_KEY])
  883. return -EINVAL;
  884. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  885. if (err)
  886. return err;
  887. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  888. if (!dp)
  889. return -ENODEV;
  890. table = genl_dereference(dp->table);
  891. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  892. if (!flow)
  893. return -ENOENT;
  894. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  895. info->snd_seq, OVS_FLOW_CMD_NEW);
  896. if (IS_ERR(reply))
  897. return PTR_ERR(reply);
  898. return genlmsg_reply(reply, info);
  899. }
  900. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  901. {
  902. struct nlattr **a = info->attrs;
  903. struct ovs_header *ovs_header = info->userhdr;
  904. struct sw_flow_key key;
  905. struct sk_buff *reply;
  906. struct sw_flow *flow;
  907. struct datapath *dp;
  908. struct flow_table *table;
  909. int err;
  910. int key_len;
  911. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  912. if (!dp)
  913. return -ENODEV;
  914. if (!a[OVS_FLOW_ATTR_KEY])
  915. return flush_flows(dp);
  916. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  917. if (err)
  918. return err;
  919. table = genl_dereference(dp->table);
  920. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  921. if (!flow)
  922. return -ENOENT;
  923. reply = ovs_flow_cmd_alloc_info(flow);
  924. if (!reply)
  925. return -ENOMEM;
  926. ovs_flow_tbl_remove(table, flow);
  927. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  928. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  929. BUG_ON(err < 0);
  930. ovs_flow_deferred_free(flow);
  931. genl_notify(reply, genl_info_net(info), info->snd_portid,
  932. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  933. return 0;
  934. }
  935. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  936. {
  937. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  938. struct datapath *dp;
  939. struct flow_table *table;
  940. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  941. if (!dp)
  942. return -ENODEV;
  943. table = genl_dereference(dp->table);
  944. for (;;) {
  945. struct sw_flow *flow;
  946. u32 bucket, obj;
  947. bucket = cb->args[0];
  948. obj = cb->args[1];
  949. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  950. if (!flow)
  951. break;
  952. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  953. NETLINK_CB(cb->skb).portid,
  954. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  955. OVS_FLOW_CMD_NEW) < 0)
  956. break;
  957. cb->args[0] = bucket;
  958. cb->args[1] = obj;
  959. }
  960. return skb->len;
  961. }
  962. static struct genl_ops dp_flow_genl_ops[] = {
  963. { .cmd = OVS_FLOW_CMD_NEW,
  964. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  965. .policy = flow_policy,
  966. .doit = ovs_flow_cmd_new_or_set
  967. },
  968. { .cmd = OVS_FLOW_CMD_DEL,
  969. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  970. .policy = flow_policy,
  971. .doit = ovs_flow_cmd_del
  972. },
  973. { .cmd = OVS_FLOW_CMD_GET,
  974. .flags = 0, /* OK for unprivileged users. */
  975. .policy = flow_policy,
  976. .doit = ovs_flow_cmd_get,
  977. .dumpit = ovs_flow_cmd_dump
  978. },
  979. { .cmd = OVS_FLOW_CMD_SET,
  980. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  981. .policy = flow_policy,
  982. .doit = ovs_flow_cmd_new_or_set,
  983. },
  984. };
  985. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  986. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  987. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  988. };
  989. static struct genl_family dp_datapath_genl_family = {
  990. .id = GENL_ID_GENERATE,
  991. .hdrsize = sizeof(struct ovs_header),
  992. .name = OVS_DATAPATH_FAMILY,
  993. .version = OVS_DATAPATH_VERSION,
  994. .maxattr = OVS_DP_ATTR_MAX,
  995. .netnsok = true
  996. };
  997. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  998. .name = OVS_DATAPATH_MCGROUP
  999. };
  1000. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1001. u32 portid, u32 seq, u32 flags, u8 cmd)
  1002. {
  1003. struct ovs_header *ovs_header;
  1004. struct ovs_dp_stats dp_stats;
  1005. int err;
  1006. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1007. flags, cmd);
  1008. if (!ovs_header)
  1009. goto error;
  1010. ovs_header->dp_ifindex = get_dpifindex(dp);
  1011. rcu_read_lock();
  1012. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1013. rcu_read_unlock();
  1014. if (err)
  1015. goto nla_put_failure;
  1016. get_dp_stats(dp, &dp_stats);
  1017. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1018. goto nla_put_failure;
  1019. return genlmsg_end(skb, ovs_header);
  1020. nla_put_failure:
  1021. genlmsg_cancel(skb, ovs_header);
  1022. error:
  1023. return -EMSGSIZE;
  1024. }
  1025. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1026. u32 seq, u8 cmd)
  1027. {
  1028. struct sk_buff *skb;
  1029. int retval;
  1030. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1031. if (!skb)
  1032. return ERR_PTR(-ENOMEM);
  1033. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1034. if (retval < 0) {
  1035. kfree_skb(skb);
  1036. return ERR_PTR(retval);
  1037. }
  1038. return skb;
  1039. }
  1040. /* Called with genl_mutex and optionally with RTNL lock also. */
  1041. static struct datapath *lookup_datapath(struct net *net,
  1042. struct ovs_header *ovs_header,
  1043. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1044. {
  1045. struct datapath *dp;
  1046. if (!a[OVS_DP_ATTR_NAME])
  1047. dp = get_dp(net, ovs_header->dp_ifindex);
  1048. else {
  1049. struct vport *vport;
  1050. rcu_read_lock();
  1051. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1052. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1053. rcu_read_unlock();
  1054. }
  1055. return dp ? dp : ERR_PTR(-ENODEV);
  1056. }
  1057. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1058. {
  1059. struct nlattr **a = info->attrs;
  1060. struct vport_parms parms;
  1061. struct sk_buff *reply;
  1062. struct datapath *dp;
  1063. struct vport *vport;
  1064. struct ovs_net *ovs_net;
  1065. int err, i;
  1066. err = -EINVAL;
  1067. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1068. goto err;
  1069. rtnl_lock();
  1070. err = -ENOMEM;
  1071. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1072. if (dp == NULL)
  1073. goto err_unlock_rtnl;
  1074. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1075. /* Allocate table. */
  1076. err = -ENOMEM;
  1077. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1078. if (!dp->table)
  1079. goto err_free_dp;
  1080. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1081. if (!dp->stats_percpu) {
  1082. err = -ENOMEM;
  1083. goto err_destroy_table;
  1084. }
  1085. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1086. GFP_KERNEL);
  1087. if (!dp->ports) {
  1088. err = -ENOMEM;
  1089. goto err_destroy_percpu;
  1090. }
  1091. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1092. INIT_HLIST_HEAD(&dp->ports[i]);
  1093. /* Set up our datapath device. */
  1094. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1095. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1096. parms.options = NULL;
  1097. parms.dp = dp;
  1098. parms.port_no = OVSP_LOCAL;
  1099. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1100. vport = new_vport(&parms);
  1101. if (IS_ERR(vport)) {
  1102. err = PTR_ERR(vport);
  1103. if (err == -EBUSY)
  1104. err = -EEXIST;
  1105. goto err_destroy_ports_array;
  1106. }
  1107. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1108. info->snd_seq, OVS_DP_CMD_NEW);
  1109. err = PTR_ERR(reply);
  1110. if (IS_ERR(reply))
  1111. goto err_destroy_local_port;
  1112. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1113. list_add_tail(&dp->list_node, &ovs_net->dps);
  1114. rtnl_unlock();
  1115. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1116. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1117. GFP_KERNEL);
  1118. return 0;
  1119. err_destroy_local_port:
  1120. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1121. err_destroy_ports_array:
  1122. kfree(dp->ports);
  1123. err_destroy_percpu:
  1124. free_percpu(dp->stats_percpu);
  1125. err_destroy_table:
  1126. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1127. err_free_dp:
  1128. release_net(ovs_dp_get_net(dp));
  1129. kfree(dp);
  1130. err_unlock_rtnl:
  1131. rtnl_unlock();
  1132. err:
  1133. return err;
  1134. }
  1135. /* Called with genl_mutex. */
  1136. static void __dp_destroy(struct datapath *dp)
  1137. {
  1138. int i;
  1139. rtnl_lock();
  1140. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1141. struct vport *vport;
  1142. struct hlist_node *n;
  1143. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1144. if (vport->port_no != OVSP_LOCAL)
  1145. ovs_dp_detach_port(vport);
  1146. }
  1147. list_del(&dp->list_node);
  1148. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1149. /* rtnl_unlock() will wait until all the references to devices that
  1150. * are pending unregistration have been dropped. We do it here to
  1151. * ensure that any internal devices (which contain DP pointers) are
  1152. * fully destroyed before freeing the datapath.
  1153. */
  1154. rtnl_unlock();
  1155. call_rcu(&dp->rcu, destroy_dp_rcu);
  1156. }
  1157. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1158. {
  1159. struct sk_buff *reply;
  1160. struct datapath *dp;
  1161. int err;
  1162. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1163. err = PTR_ERR(dp);
  1164. if (IS_ERR(dp))
  1165. return err;
  1166. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1167. info->snd_seq, OVS_DP_CMD_DEL);
  1168. err = PTR_ERR(reply);
  1169. if (IS_ERR(reply))
  1170. return err;
  1171. __dp_destroy(dp);
  1172. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1173. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1174. GFP_KERNEL);
  1175. return 0;
  1176. }
  1177. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1178. {
  1179. struct sk_buff *reply;
  1180. struct datapath *dp;
  1181. int err;
  1182. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1183. if (IS_ERR(dp))
  1184. return PTR_ERR(dp);
  1185. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1186. info->snd_seq, OVS_DP_CMD_NEW);
  1187. if (IS_ERR(reply)) {
  1188. err = PTR_ERR(reply);
  1189. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1190. ovs_dp_datapath_multicast_group.id, err);
  1191. return 0;
  1192. }
  1193. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1194. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1195. GFP_KERNEL);
  1196. return 0;
  1197. }
  1198. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1199. {
  1200. struct sk_buff *reply;
  1201. struct datapath *dp;
  1202. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1203. if (IS_ERR(dp))
  1204. return PTR_ERR(dp);
  1205. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1206. info->snd_seq, OVS_DP_CMD_NEW);
  1207. if (IS_ERR(reply))
  1208. return PTR_ERR(reply);
  1209. return genlmsg_reply(reply, info);
  1210. }
  1211. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1212. {
  1213. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1214. struct datapath *dp;
  1215. int skip = cb->args[0];
  1216. int i = 0;
  1217. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1218. if (i >= skip &&
  1219. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1220. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1221. OVS_DP_CMD_NEW) < 0)
  1222. break;
  1223. i++;
  1224. }
  1225. cb->args[0] = i;
  1226. return skb->len;
  1227. }
  1228. static struct genl_ops dp_datapath_genl_ops[] = {
  1229. { .cmd = OVS_DP_CMD_NEW,
  1230. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1231. .policy = datapath_policy,
  1232. .doit = ovs_dp_cmd_new
  1233. },
  1234. { .cmd = OVS_DP_CMD_DEL,
  1235. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1236. .policy = datapath_policy,
  1237. .doit = ovs_dp_cmd_del
  1238. },
  1239. { .cmd = OVS_DP_CMD_GET,
  1240. .flags = 0, /* OK for unprivileged users. */
  1241. .policy = datapath_policy,
  1242. .doit = ovs_dp_cmd_get,
  1243. .dumpit = ovs_dp_cmd_dump
  1244. },
  1245. { .cmd = OVS_DP_CMD_SET,
  1246. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1247. .policy = datapath_policy,
  1248. .doit = ovs_dp_cmd_set,
  1249. },
  1250. };
  1251. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1252. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1253. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1254. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1255. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1256. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1257. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1258. };
  1259. static struct genl_family dp_vport_genl_family = {
  1260. .id = GENL_ID_GENERATE,
  1261. .hdrsize = sizeof(struct ovs_header),
  1262. .name = OVS_VPORT_FAMILY,
  1263. .version = OVS_VPORT_VERSION,
  1264. .maxattr = OVS_VPORT_ATTR_MAX,
  1265. .netnsok = true
  1266. };
  1267. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1268. .name = OVS_VPORT_MCGROUP
  1269. };
  1270. /* Called with RTNL lock or RCU read lock. */
  1271. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1272. u32 portid, u32 seq, u32 flags, u8 cmd)
  1273. {
  1274. struct ovs_header *ovs_header;
  1275. struct ovs_vport_stats vport_stats;
  1276. int err;
  1277. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1278. flags, cmd);
  1279. if (!ovs_header)
  1280. return -EMSGSIZE;
  1281. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1282. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1283. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1284. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1285. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1286. goto nla_put_failure;
  1287. ovs_vport_get_stats(vport, &vport_stats);
  1288. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1289. &vport_stats))
  1290. goto nla_put_failure;
  1291. err = ovs_vport_get_options(vport, skb);
  1292. if (err == -EMSGSIZE)
  1293. goto error;
  1294. return genlmsg_end(skb, ovs_header);
  1295. nla_put_failure:
  1296. err = -EMSGSIZE;
  1297. error:
  1298. genlmsg_cancel(skb, ovs_header);
  1299. return err;
  1300. }
  1301. /* Called with RTNL lock or RCU read lock. */
  1302. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1303. u32 seq, u8 cmd)
  1304. {
  1305. struct sk_buff *skb;
  1306. int retval;
  1307. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1308. if (!skb)
  1309. return ERR_PTR(-ENOMEM);
  1310. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1311. if (retval < 0) {
  1312. kfree_skb(skb);
  1313. return ERR_PTR(retval);
  1314. }
  1315. return skb;
  1316. }
  1317. /* Called with RTNL lock or RCU read lock. */
  1318. static struct vport *lookup_vport(struct net *net,
  1319. struct ovs_header *ovs_header,
  1320. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1321. {
  1322. struct datapath *dp;
  1323. struct vport *vport;
  1324. if (a[OVS_VPORT_ATTR_NAME]) {
  1325. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1326. if (!vport)
  1327. return ERR_PTR(-ENODEV);
  1328. if (ovs_header->dp_ifindex &&
  1329. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1330. return ERR_PTR(-ENODEV);
  1331. return vport;
  1332. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1333. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1334. if (port_no >= DP_MAX_PORTS)
  1335. return ERR_PTR(-EFBIG);
  1336. dp = get_dp(net, ovs_header->dp_ifindex);
  1337. if (!dp)
  1338. return ERR_PTR(-ENODEV);
  1339. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1340. if (!vport)
  1341. return ERR_PTR(-ENOENT);
  1342. return vport;
  1343. } else
  1344. return ERR_PTR(-EINVAL);
  1345. }
  1346. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1347. {
  1348. struct nlattr **a = info->attrs;
  1349. struct ovs_header *ovs_header = info->userhdr;
  1350. struct vport_parms parms;
  1351. struct sk_buff *reply;
  1352. struct vport *vport;
  1353. struct datapath *dp;
  1354. u32 port_no;
  1355. int err;
  1356. err = -EINVAL;
  1357. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1358. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1359. goto exit;
  1360. rtnl_lock();
  1361. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1362. err = -ENODEV;
  1363. if (!dp)
  1364. goto exit_unlock;
  1365. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1366. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1367. err = -EFBIG;
  1368. if (port_no >= DP_MAX_PORTS)
  1369. goto exit_unlock;
  1370. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1371. err = -EBUSY;
  1372. if (vport)
  1373. goto exit_unlock;
  1374. } else {
  1375. for (port_no = 1; ; port_no++) {
  1376. if (port_no >= DP_MAX_PORTS) {
  1377. err = -EFBIG;
  1378. goto exit_unlock;
  1379. }
  1380. vport = ovs_vport_rtnl(dp, port_no);
  1381. if (!vport)
  1382. break;
  1383. }
  1384. }
  1385. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1386. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1387. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1388. parms.dp = dp;
  1389. parms.port_no = port_no;
  1390. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1391. vport = new_vport(&parms);
  1392. err = PTR_ERR(vport);
  1393. if (IS_ERR(vport))
  1394. goto exit_unlock;
  1395. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1396. OVS_VPORT_CMD_NEW);
  1397. if (IS_ERR(reply)) {
  1398. err = PTR_ERR(reply);
  1399. ovs_dp_detach_port(vport);
  1400. goto exit_unlock;
  1401. }
  1402. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1403. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1404. exit_unlock:
  1405. rtnl_unlock();
  1406. exit:
  1407. return err;
  1408. }
  1409. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1410. {
  1411. struct nlattr **a = info->attrs;
  1412. struct sk_buff *reply;
  1413. struct vport *vport;
  1414. int err;
  1415. rtnl_lock();
  1416. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1417. err = PTR_ERR(vport);
  1418. if (IS_ERR(vport))
  1419. goto exit_unlock;
  1420. err = 0;
  1421. if (a[OVS_VPORT_ATTR_TYPE] &&
  1422. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1423. err = -EINVAL;
  1424. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1425. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1426. if (err)
  1427. goto exit_unlock;
  1428. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1429. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1430. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1431. OVS_VPORT_CMD_NEW);
  1432. if (IS_ERR(reply)) {
  1433. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1434. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1435. goto exit_unlock;
  1436. }
  1437. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1438. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1439. exit_unlock:
  1440. rtnl_unlock();
  1441. return err;
  1442. }
  1443. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1444. {
  1445. struct nlattr **a = info->attrs;
  1446. struct sk_buff *reply;
  1447. struct vport *vport;
  1448. int err;
  1449. rtnl_lock();
  1450. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1451. err = PTR_ERR(vport);
  1452. if (IS_ERR(vport))
  1453. goto exit_unlock;
  1454. if (vport->port_no == OVSP_LOCAL) {
  1455. err = -EINVAL;
  1456. goto exit_unlock;
  1457. }
  1458. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1459. OVS_VPORT_CMD_DEL);
  1460. err = PTR_ERR(reply);
  1461. if (IS_ERR(reply))
  1462. goto exit_unlock;
  1463. ovs_dp_detach_port(vport);
  1464. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1465. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1466. exit_unlock:
  1467. rtnl_unlock();
  1468. return err;
  1469. }
  1470. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1471. {
  1472. struct nlattr **a = info->attrs;
  1473. struct ovs_header *ovs_header = info->userhdr;
  1474. struct sk_buff *reply;
  1475. struct vport *vport;
  1476. int err;
  1477. rcu_read_lock();
  1478. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1479. err = PTR_ERR(vport);
  1480. if (IS_ERR(vport))
  1481. goto exit_unlock;
  1482. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1483. OVS_VPORT_CMD_NEW);
  1484. err = PTR_ERR(reply);
  1485. if (IS_ERR(reply))
  1486. goto exit_unlock;
  1487. rcu_read_unlock();
  1488. return genlmsg_reply(reply, info);
  1489. exit_unlock:
  1490. rcu_read_unlock();
  1491. return err;
  1492. }
  1493. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1494. {
  1495. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1496. struct datapath *dp;
  1497. int bucket = cb->args[0], skip = cb->args[1];
  1498. int i, j = 0;
  1499. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1500. if (!dp)
  1501. return -ENODEV;
  1502. rcu_read_lock();
  1503. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1504. struct vport *vport;
  1505. j = 0;
  1506. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1507. if (j >= skip &&
  1508. ovs_vport_cmd_fill_info(vport, skb,
  1509. NETLINK_CB(cb->skb).portid,
  1510. cb->nlh->nlmsg_seq,
  1511. NLM_F_MULTI,
  1512. OVS_VPORT_CMD_NEW) < 0)
  1513. goto out;
  1514. j++;
  1515. }
  1516. skip = 0;
  1517. }
  1518. out:
  1519. rcu_read_unlock();
  1520. cb->args[0] = i;
  1521. cb->args[1] = j;
  1522. return skb->len;
  1523. }
  1524. static struct genl_ops dp_vport_genl_ops[] = {
  1525. { .cmd = OVS_VPORT_CMD_NEW,
  1526. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1527. .policy = vport_policy,
  1528. .doit = ovs_vport_cmd_new
  1529. },
  1530. { .cmd = OVS_VPORT_CMD_DEL,
  1531. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1532. .policy = vport_policy,
  1533. .doit = ovs_vport_cmd_del
  1534. },
  1535. { .cmd = OVS_VPORT_CMD_GET,
  1536. .flags = 0, /* OK for unprivileged users. */
  1537. .policy = vport_policy,
  1538. .doit = ovs_vport_cmd_get,
  1539. .dumpit = ovs_vport_cmd_dump
  1540. },
  1541. { .cmd = OVS_VPORT_CMD_SET,
  1542. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1543. .policy = vport_policy,
  1544. .doit = ovs_vport_cmd_set,
  1545. },
  1546. };
  1547. struct genl_family_and_ops {
  1548. struct genl_family *family;
  1549. struct genl_ops *ops;
  1550. int n_ops;
  1551. struct genl_multicast_group *group;
  1552. };
  1553. static const struct genl_family_and_ops dp_genl_families[] = {
  1554. { &dp_datapath_genl_family,
  1555. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1556. &ovs_dp_datapath_multicast_group },
  1557. { &dp_vport_genl_family,
  1558. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1559. &ovs_dp_vport_multicast_group },
  1560. { &dp_flow_genl_family,
  1561. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1562. &ovs_dp_flow_multicast_group },
  1563. { &dp_packet_genl_family,
  1564. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1565. NULL },
  1566. };
  1567. static void dp_unregister_genl(int n_families)
  1568. {
  1569. int i;
  1570. for (i = 0; i < n_families; i++)
  1571. genl_unregister_family(dp_genl_families[i].family);
  1572. }
  1573. static int dp_register_genl(void)
  1574. {
  1575. int n_registered;
  1576. int err;
  1577. int i;
  1578. n_registered = 0;
  1579. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1580. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1581. err = genl_register_family_with_ops(f->family, f->ops,
  1582. f->n_ops);
  1583. if (err)
  1584. goto error;
  1585. n_registered++;
  1586. if (f->group) {
  1587. err = genl_register_mc_group(f->family, f->group);
  1588. if (err)
  1589. goto error;
  1590. }
  1591. }
  1592. return 0;
  1593. error:
  1594. dp_unregister_genl(n_registered);
  1595. return err;
  1596. }
  1597. static void rehash_flow_table(struct work_struct *work)
  1598. {
  1599. struct datapath *dp;
  1600. struct net *net;
  1601. genl_lock();
  1602. rtnl_lock();
  1603. for_each_net(net) {
  1604. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1605. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1606. struct flow_table *old_table = genl_dereference(dp->table);
  1607. struct flow_table *new_table;
  1608. new_table = ovs_flow_tbl_rehash(old_table);
  1609. if (!IS_ERR(new_table)) {
  1610. rcu_assign_pointer(dp->table, new_table);
  1611. ovs_flow_tbl_deferred_destroy(old_table);
  1612. }
  1613. }
  1614. }
  1615. rtnl_unlock();
  1616. genl_unlock();
  1617. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1618. }
  1619. static int __net_init ovs_init_net(struct net *net)
  1620. {
  1621. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1622. INIT_LIST_HEAD(&ovs_net->dps);
  1623. return 0;
  1624. }
  1625. static void __net_exit ovs_exit_net(struct net *net)
  1626. {
  1627. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1628. struct datapath *dp, *dp_next;
  1629. genl_lock();
  1630. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1631. __dp_destroy(dp);
  1632. genl_unlock();
  1633. }
  1634. static struct pernet_operations ovs_net_ops = {
  1635. .init = ovs_init_net,
  1636. .exit = ovs_exit_net,
  1637. .id = &ovs_net_id,
  1638. .size = sizeof(struct ovs_net),
  1639. };
  1640. static int __init dp_init(void)
  1641. {
  1642. int err;
  1643. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1644. pr_info("Open vSwitch switching datapath\n");
  1645. err = ovs_flow_init();
  1646. if (err)
  1647. goto error;
  1648. err = ovs_vport_init();
  1649. if (err)
  1650. goto error_flow_exit;
  1651. err = register_pernet_device(&ovs_net_ops);
  1652. if (err)
  1653. goto error_vport_exit;
  1654. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1655. if (err)
  1656. goto error_netns_exit;
  1657. err = dp_register_genl();
  1658. if (err < 0)
  1659. goto error_unreg_notifier;
  1660. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1661. return 0;
  1662. error_unreg_notifier:
  1663. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1664. error_netns_exit:
  1665. unregister_pernet_device(&ovs_net_ops);
  1666. error_vport_exit:
  1667. ovs_vport_exit();
  1668. error_flow_exit:
  1669. ovs_flow_exit();
  1670. error:
  1671. return err;
  1672. }
  1673. static void dp_cleanup(void)
  1674. {
  1675. cancel_delayed_work_sync(&rehash_flow_wq);
  1676. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1677. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1678. unregister_pernet_device(&ovs_net_ops);
  1679. rcu_barrier();
  1680. ovs_vport_exit();
  1681. ovs_flow_exit();
  1682. }
  1683. module_init(dp_init);
  1684. module_exit(dp_cleanup);
  1685. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1686. MODULE_LICENSE("GPL");